2
2
3
3
Yet another Go logger library.
4
4
5
+ ### Version 2
6
+
7
+ The new version of this library implements some breaking changes:
8
+
9
+ * The initialization options contains some changes. Once the logger is initialized, the rest of the code should be
10
+ compatible with the ` v1 ` version.
11
+ * Added per-target logging level. Now you can optionally specify a different logging level for console and file logging.
12
+ For example, you can just send limited logs to a third-party provider to avoid high costs and keep a detailed local
13
+ file log for debugging purposes.
14
+ * ` timestamp ` and ` level ` fields are always added to a JSON log. DO NOT include them when the input is a struct.
15
+ * Verbosity level for the default logger is now ` Info ` instead of ` Warning ` .
16
+
5
17
## How to use
6
18
7
19
1 . Import the library
8
20
9
21
``` golang
10
22
import (
11
- gologger " github.com/randlabs/go-logger"
23
+ logger " github.com/randlabs/go-logger/v2 "
12
24
)
13
25
```
14
26
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()` .
27
+ 2 . Then use ` logger .Create` to create a logger object with desired options.
28
+ 3 . Optionally, you can also use the default logger which outputs only to console by accessing ` logger .Default()` .
17
29
18
30
## Logger options:
19
31
20
32
The ` Options ` struct accepts several modifiers that affects the logger behavior:
21
33
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. |
34
+ | Field | Meaning |
35
+ | ----------------| ---------------------------------------------------------|
36
+ | ` Console ` | Establishes some options for the console output. |
37
+ | ` File ` | Enable file logging. Optional. Details below. |
38
+ | ` SysLog ` | Enable SysLog logging. Optional. Details below. |
39
+ | ` Level ` | Set the initial logging level to use. |
40
+ | ` DebugLevel ` | Set the initial logging level for debug output to use. |
41
+ | ` UseLocalTime ` | Use the local computer time instead of UTC. |
42
+ | ` ErrorHandler ` | A callback to call if an internal error is encountered. |
31
43
32
- The ` FileOptions ` struct accepts the following parameters:
44
+ #### ConsoleOptions:
45
+
46
+ | Field | Meaning |
47
+ | --------------| -----------------------------------------------------------------------|
48
+ | ` Disable ` | Disabled console output. |
49
+ | ` Level ` | Optional logging level to use in the console output. |
50
+ | ` DebugLevel ` | Optional logging level for debug output to use in the console output. |
51
+
52
+ #### FileOptions:
33
53
34
54
| Field | Meaning |
35
55
| --------------| -----------------------------------------------------------------------------|
36
56
| ` Prefix ` | Filename prefix to use when a file is created. Defaults to the binary name. |
37
57
| ` 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:
58
+ | ` DaysToKeep ` | Amount of days to keep old logs. |
59
+ | ` Level ` | Optional logging level to use in the file output. |
60
+ | ` DebugLevel ` | Optional logging level for debug output to use in the file output. |
41
61
62
+ #### SysLogOptions:
42
63
43
64
| Field | Meaning |
44
65
| -----------------------| -------------------------------------------------------------------------------------------|
@@ -50,6 +71,8 @@ And the `SysLogOptions` struct accepts the following parameters:
50
71
| ` UseRFC5424 ` | Send messages in the new RFC 5424 format instead of the original RFC 3164 specification. |
51
72
| ` MaxMessageQueueSize ` | Set the maximum amount of messages to keep in memory if connection to the server is lost. |
52
73
| ` TlsConfig ` | An optional pointer to a ` tls.Config ` object to provide the TLS configuration for use. |
74
+ | ` Level ` | Optional logging level to use in the syslog output. |
75
+ | ` DebugLevel ` | Optional logging level for debug output to use in the syslog output. |
53
76
54
77
## Example
55
78
@@ -58,10 +81,8 @@ package example
58
81
59
82
import (
60
83
" fmt"
61
- " os"
62
- " path/filepath"
63
84
64
- gologger " github.com/randlabs/go-logger"
85
+ logger " github.com/randlabs/go-logger/v2 "
65
86
)
66
87
67
88
// Define a custom JSON message. Timestamp and level will be automatically added by the logger.
@@ -71,43 +92,43 @@ type JsonMessage struct {
71
92
72
93
func main () {
73
94
// Create the logger
74
- logger , err := gologger .Create (gologger .Options {
75
- FileLog : &gologger .FileOptions {
95
+ lg , err := logger .Create (logger .Options {
96
+ File : &logger .FileOptions {
76
97
Directory: " ./logs" ,
77
98
DaysToKeep: 7 ,
78
99
},
79
- Level: gologger .LogLevelDebug ,
100
+ Level: logger .LogLevelDebug ,
80
101
DebugLevel: 1 ,
81
102
})
82
103
if err != nil {
83
104
// Use default logger to send the error
84
- gologger .Default ().Error (fmt.Sprintf (" unable to initialize. [%v ]" , err))
105
+ logger .Default ().Error (fmt.Sprintf (" unable to initialize. [%v ]" , err))
85
106
return
86
107
}
87
108
// Defer logger shut down
88
- defer logger .Destroy ()
109
+ defer lg .Destroy ()
89
110
90
111
// 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" )
112
+ lg .Error (" This is an error message sample" )
113
+ lg .Warning (" This is a warning message sample" )
114
+ lg .Info (" This is an information message sample" )
115
+ lg .Debug (1 , " This is a debug message sample at level 1 which should be printed" )
116
+ lg .Debug (2 , " This is a debug message sample at level 2 which should NOT be printed" )
96
117
97
118
// Send some other logs using the JSON format
98
- logger .Error (JsonMessage{
119
+ lg .Error (JsonMessage{
99
120
Message: " This is an error message sample" ,
100
121
})
101
- logger .Warning (JsonMessage{
122
+ lg .Warning (JsonMessage{
102
123
Message: " This is a warning message sample" ,
103
124
})
104
- logger .Info (JsonMessage{
125
+ lg .Info (JsonMessage{
105
126
Message: " This is an information message sample" ,
106
127
})
107
- logger .Debug (1 , JsonMessage{
128
+ lg .Debug (1 , JsonMessage{
108
129
Message: " This is a debug message sample at level 1 which should be printed" ,
109
130
})
110
- logger .Debug (2 , JsonMessage{
131
+ lg .Debug (2 , JsonMessage{
111
132
Message: " This is a debug message sample at level 2 which should NOT be printed" ,
112
133
})
113
134
}
0 commit comments