Documentation
¶
Overview ¶
Package journal implements a very simple logging facility with only one shared logging method, namely "Log".
Index ¶
- Constants
- func ConnectToJournald(host string, port int, service, instance, token string, timeout time.Duration) (io.WriteCloser, error)
- func ConnectToKafka(host string, port int, topic string) (io.WriteCloser, error)
- type Code
- type Config
- type Logger
- func (l *Logger) AddDestination(name string, writer io.Writer) error
- func (l *Logger) ListDestinations() []string
- func (l *Logger) Log(caller string, code int, msg string, format ...interface{}) error
- func (l *Logger) LogFields(caller string, code int, msg map[string]interface{}) error
- func (l *Logger) NewCaller(caller string) func(int, string, ...interface{}) error
- func (l *Logger) NewCallerCode(caller string, code int) func(string, ...interface{}) error
- func (l *Logger) Quit()
- func (l *Logger) RawEntry(entry map[int64]string) error
- func (l *Logger) RemoveDestination(name string) error
- func (l *Logger) UseCustomCodes(codes map[int]Code)
Examples ¶
Constants ¶
const ( ROT_NONE = 0 ROT_DAILY = 1 ROT_WEEKLY = 2 ROT_MONTHLY = 3 ROT_ANNUALLY = 4 )
File rotation frequency
const ( OUT_FILE = 0 OUT_STDOUT = 1 OUT_FILE_AND_STDOUT = 2 )
Output selection
const ( COL_DATE_YYMMDD = 0 COL_DATE_YYMMDD_HHMMSS = 1 COL_DATE_YYMMDD_HHMMSS_NANO = 2 COL_TIMESTAMP = 3 COL_SERVICE = 4 COL_INSTANCE = 5 COL_CALLER = 6 COL_MSG_TYPE_SHORT = 7 COL_MSG_TYPE_INT = 8 COL_MSG_TYPE_STR = 9 COL_MSG = 10 COL_FILE = 11 COL_LINE = 12 )
Log columns
Variables ¶
This section is empty.
Functions ¶
func ConnectToJournald ¶
func ConnectToJournald(host string, port int, service, instance, token string, timeout time.Duration) (io.WriteCloser, error)
ConnectToJournald connects to a log server backend
func ConnectToKafka ¶
ConnectToKafka connects to a kafka backend as a producer
Types ¶
type Code ¶
Code contains a single message type with an indicator of whether this message should be treated as an error.
type Config ¶
type Config struct {
Service string // Service name
Instance string // Instance name
Folder string // Folder to store logfiles (can be empty if logging to stdout only)
Filename string // Filename of the logfiles (without date suffix and file extension. Can be empty if logging to stdout only)
Rotation int // Logfile rotation frequency
Out int // Logger output type
Headers bool // Should the logfile contain column headers?
JSON bool // Should each entry be written as a JSON-formatted string?
Compress bool // Should old logfiles be compressed?
Columns []int64 // List of relevant columns (can be empty if default columns should be used)
}
Config contains all the necessary settings to create a new local logging facility
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
Logger is the main loggger struct
func (*Logger) AddDestination ¶
AddDestination adds a (remote) destination to send logs to
func (*Logger) ListDestinations ¶
ListDestinations lists all (remote) destinations
func (*Logger) Log ¶
Log logs a simple message and returns nil or error, depending on the code
Example ¶
Simple example of local logging
package main
import (
"fmt"
"io/ioutil"
"os"
"testing"
)
// Test/example setup
func setup(t *testing.T) (tempdir string, teardown func()) {
dir, err := ioutil.TempDir(os.Getenv("HOME"), "example")
if err != nil {
t.Errorf("Could not create tempdir: %s", err.Error())
}
return dir, func() {
os.RemoveAll(dir)
}
}
// Simple example of local logging
func main() {
// Create temporary folder and teardown function
tempdir, teardown := setup(&testing.T{})
defer teardown()
// Instantiate logger
logger, err := New(&Config{
Service: "MyService",
Instance: "MyInstance",
Folder: tempdir,
Filename: "myservice",
Rotation: ROT_DAILY,
Out: OUT_FILE_AND_STDOUT,
Headers: true,
JSON: false,
Compress: true,
Columns: []int64{}, // Use default columns
})
if err != nil {
fmt.Printf("Could not start logger: %s", err.Error())
os.Exit(1)
}
// Log messages
notify := logger.NewCaller("Example 1")
notify(0, "Hello, World!")
}
func (*Logger) LogFields ¶
LogFields encodes the message (not the whole log) in JSON and writes to log
func (*Logger) NewCallerCode ¶
NewCallerCode is a wrapper for the Logger.fullog function
func (*Logger) RawEntry ¶
RawEntry writes a raw log entry (map of strings) into the ledger. The raw entry must contain columns COL_DATE_YYMMDD_HHMMSS_NANO to COL_LINE
func (*Logger) RemoveDestination ¶
RemoveDestination removes a (remote) destination to send logs to
func (*Logger) UseCustomCodes ¶
UseCustomCodes Replaces loggers default message codes with custom ones