Skip to content

Commit 10c9809

Browse files
authored
Merge pull request #12 from randlabs/docs
Added documentation
2 parents 3b5c56d + 4e2188b commit 10c9809

File tree

2 files changed

+117
-3
lines changed

2 files changed

+117
-3
lines changed

README.md

Lines changed: 115 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,117 @@
11
# go-logger
22

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.

internal/syslog/syslog.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,13 @@ type Options struct {
5858
// Application name to use. Defaults to the binary name.
5959
AppName string `json:"appName,omitempty"`
6060

61-
// Syslog server host name
61+
// Syslog server host name.
6262
Host string `json:"host,omitempty"`
6363

6464
// Syslog server port. Defaults to 514, 1468 or 6514 depending on the network protocol used.
6565
Port uint16 `json:"port,omitempty"`
6666

67-
// Use TCP instead of UDP
67+
// Use TCP instead of UDP.
6868
UseTcp bool `json:"useTcp,omitempty"`
6969

7070
// Uses a secure connection. Implies TCP.

0 commit comments

Comments
 (0)