|
1 | 1 | # go-logger
|
2 | 2 |
|
3 |
| -Simple logger used mostly in Randlabs.IO apps |
| 3 | +Yet another Go logger library. |
| 4 | + |
| 5 | +## How to use |
| 6 | + |
| 7 | +1. Import the library |
| 8 | + |
| 9 | +```golang |
| 10 | +import ( |
| 11 | + gologger "github.com/randlabs/go-logger" |
| 12 | +) |
| 13 | +``` |
| 14 | + |
| 15 | +2. Then use `gologger.Create` to create a logger object with desired options. |
| 16 | +3. Optionally, you can also use the default logger which outputs only to console by accesing `gologger.Default()`. |
| 17 | + |
| 18 | +## Logger options: |
| 19 | + |
| 20 | +The `Options` struct accepts several modifiers that affects the logger behavior: |
| 21 | + |
| 22 | +| Field | Meaning | |
| 23 | +|------------------|---------------------------------------------------------| |
| 24 | +| `DisableConsole` | Disable console output. | |
| 25 | +| `FileLog` | Enable file logging. Optional. Details below. | |
| 26 | +| `SysLog` | Enable SysLog logging. Optional. Details below. | |
| 27 | +| `Level` | Set the initial logging level to use. | |
| 28 | +| `DebugLevel` | Set the initial logging level for debug output to use. | |
| 29 | +| `UseLocalTime` | Use the local computer time instead of UTC. | |
| 30 | +| `ErrorHandler` | A callback to call if an internal error is encountered. | |
| 31 | + |
| 32 | +The `FileOptions` struct accepts the following parameters: |
| 33 | + |
| 34 | +| Field | Meaning | |
| 35 | +|--------------|-----------------------------------------------------------------------------| |
| 36 | +| `Prefix` | Filename prefix to use when a file is created. Defaults to the binary name. | |
| 37 | +| `Directory` | Destination directory to store log files. | |
| 38 | +| `DaysToKeep` | Amount of days to keep old logs, | |
| 39 | + |
| 40 | +And the `SysLogOptions` struct accepts the following parameters: |
| 41 | + |
| 42 | + |
| 43 | +| Field | Meaning | |
| 44 | +|-----------------------|-------------------------------------------------------------------------------------------| |
| 45 | +| `AppName` | Application name to use. Defaults to the binary name. | |
| 46 | +| `Host` | Syslog server host name. | |
| 47 | +| `Port` | Syslog server port. Defaults to 514, 1468 or 6514 depending on the network protocol used. | |
| 48 | +| `UseTcp` | Use TCP instead of UDP. | |
| 49 | +| `UseTls` | Uses a secure connection. Implies TCP. | |
| 50 | +| `UseRFC5424` | Send messages in the new RFC 5424 format instead of the original RFC 3164 specification. | |
| 51 | +| `MaxMessageQueueSize` | Set the maximum amount of messages to keep in memory if connection to the server is lost. | |
| 52 | +| `TlsConfig` | An optional pointer to a `tls.Config` object to provide the TLS configuration for use. | |
| 53 | + |
| 54 | +## Example |
| 55 | + |
| 56 | +```golang |
| 57 | +package example |
| 58 | + |
| 59 | +import ( |
| 60 | + "fmt" |
| 61 | + "os" |
| 62 | + "path/filepath" |
| 63 | + |
| 64 | + gologger "github.com/randlabs/go-logger" |
| 65 | +) |
| 66 | + |
| 67 | +// Define a custom JSON message. Timestamp and level will be automatically added by the logger. |
| 68 | +type JsonMessage struct { |
| 69 | + Message string `json:"message"` |
| 70 | +} |
| 71 | + |
| 72 | +func main() { |
| 73 | + // Create the logger |
| 74 | + logger, err := gologger.Create(gologger.Options{ |
| 75 | + FileLog: &gologger.FileOptions{ |
| 76 | + Directory: "./logs", |
| 77 | + DaysToKeep: 7, |
| 78 | + }, |
| 79 | + Level: gologger.LogLevelDebug, |
| 80 | + DebugLevel: 1, |
| 81 | + }) |
| 82 | + if err != nil { |
| 83 | + // Use default logger to send the error |
| 84 | + gologger.Default().Error(fmt.Sprintf("unable to initialize. [%v]", err)) |
| 85 | + return |
| 86 | + } |
| 87 | + // Defer logger shut down |
| 88 | + defer logger.Destroy() |
| 89 | + |
| 90 | + // Send some logs using the plain text format |
| 91 | + logger.Error("This is an error message sample") |
| 92 | + logger.Warning("This is a warning message sample") |
| 93 | + logger.Info("This is an information message sample") |
| 94 | + logger.Debug(1, "This is a debug message sample at level 1 which should be printed") |
| 95 | + logger.Debug(2, "This is a debug message sample at level 2 which should NOT be printed") |
| 96 | + |
| 97 | + // Send some other logs using the JSON format |
| 98 | + logger.Error(JsonMessage{ |
| 99 | + Message: "This is an error message sample", |
| 100 | + }) |
| 101 | + logger.Warning(JsonMessage{ |
| 102 | + Message: "This is a warning message sample", |
| 103 | + }) |
| 104 | + logger.Info(JsonMessage{ |
| 105 | + Message: "This is an information message sample", |
| 106 | + }) |
| 107 | + logger.Debug(1, JsonMessage{ |
| 108 | + Message: "This is a debug message sample at level 1 which should be printed", |
| 109 | + }) |
| 110 | + logger.Debug(2, JsonMessage{ |
| 111 | + Message: "This is a debug message sample at level 2 which should NOT be printed", |
| 112 | + }) |
| 113 | +} |
| 114 | +``` |
| 115 | + |
| 116 | +## License |
| 117 | +See `LICENSE` file for details. |
0 commit comments