Go library for logging to LogDNA
🚧 Work in progress 🚧
go get github.com/logdna/logdna-go
import (
"github.com/logdna/logdna-go/logger"
)
func main() {
key := "YOUR INGESTION KEY HERE"
// Configure your options with your desired level, hostname, app, ip address, mac address and environment.
// Hostname is the only required field in your options- the rest are optional.
options := logger.Options{}
options.Level = "fatal"
options.Hostname = "gotest"
options.App = "myapp"
options.IPAddress = "10.0.1.101"
options.MacAddress = "C0:FF:EE:C0:FF:EE"
options.Env = "production"
options.Tags = "logging,golang"
myLogger, err := logger.CreateLogger(options, key)
}
Required
After initial setup, logging looks like this:
func main() {
...
myLogger, err := logger.CreateLogger(options, key)
myLogger.Log("Message 1")
myLogger.Close()
// Can also use Go's short-hand syntax for initializing structs to define all your options in just a single line:
options = logger.Options{Level: "error", Hostname: "gotest", App: "myapp", IPAddress: "10.0.1.101", MacAddress: "C0:FF:EE:C0:FF:EE"}
myLogger2, err := logger.CreateLogger(options, key)
myLogger2.Log("Message 2")
// Configure options with specific logs
newOptions := logger.Options{Level: "warning", Hostname: "gotest", App: "myotherapp", IPAddress: "10.0.1.101", MacAddress: "C0:FF:EE:C0:FF:EE"}
errWithOpts := myLogger2.LogWithOptions("Message 3", newOptions)
// We support the following 6 levels
myLogger2.Info("Message 1")
myLogger2.Warn("Message 2")
myLogger2.Debug("Message 3")
myLogger2.Error("Message 4")
myLogger2.Fatal("Message 5")
myLogger2.Critical("Message 6")
// To add metadata to every log-line created by the logger instance:
options.Meta = `{"key": "value", "key2": "value2"}`
myLogger3, err := logger.CreateLogger(options, key)
myLogger3.Log("Message 7")
myLogger3.Close()
}
You will see these logs in your LogDNA dashboard! Make sure to run .Close() when done with using the logger.
Run all tests in the test suite:
go test ./logger
Run a specific test:
go test ./logger -run ^TestLogger_LogWithOptions$
For more information on testing see: https://golang.org/pkg/testing/
- Optional
- Type:
string
- Default:
''
- Example Values:
YourCustomApp
- Max Length:
80
Arbitrary app name for labeling each message.
- Optional
- Type:
string
- Default:
''
- Example Values:
YourCustomEnvironment
- Max Length:
80
An environment label attached to each message.
- Optional
- Type:
time.duration
- Default:
250 * time.Millisecond
- Example Values:
10 * time.Second
Time to wait before sending the buffer.
- Optional
- Type:
string
- Default:
''
- Example Values:
YourCustomHostname
- Max Length:
80
Hostname for each HTTP request.
- Optional
- Type:
bool
- Default:
false
- Example Values:
true
Controls whether meta data for each message is searchable.
- Optional
- Type:
string
- Default:
https://logs.logdna.com/logs/ingest
URL of the logging server.
- Optional
- Type:
string
- Default:
''
- Example Values:
10.0.0.1
IPv4 or IPv6 address for each HTTP request.
- Optional
- Type:
string
- Default:
Info
- Example Values:
Debug
,Trace
,Info
,Warn
,Error
,Fatal
,YourCustomLevel
- Max Length:
80
Level to be used if not specified elsewhere.
- Optional
- Type:
string
- Default:
''
- Example Values:
c0:ff:ee:c0:ff:ee
MAC address for each HTTP request.
- Optional
- Type:
int
- Default:
50
- Example Values:
10
Maximum total line lengths before a flush is forced.
- Optional
- Type:
string
Global metadata. Added to each message, unless overridden.
- Optional
- Type:
time.Duration
- Default:
30 * time.Second
- Example Values:
10
Time limit in seconds to wait for each HTTP request before timing out.
- Optional
- Type:
string
- Default:
5
- Example Values:
logging,golang
Tags to be added to each message.
- Optional
- Type:
time.Time
- Default Values:
time.Now()
- Example Values:
time.Now()
Epoch ms time to use if not provided elsewhere.
- Required
- Type:
string
- Default:
''
Text of the log entry.
- Required
- Type:
string
- Default:
''
Text of the log entry.
- Optional
- Type:
string
- Default:
''
- Example Values:
YourCustomApp
- Max Length:
80
App name to use for the current message.
- Optional
- Type:
string
- Default:
''
- Example Values:
YourCustomEnvironment
- Max Length:
80
Environment name to use for the current message.
- Optional
- Type:
time.duration
- Default:
250 * time.Millisecond
- Example Values:
10 * time.Second
Time to wait before sending the buffer.
- Optional
- Type:
string
- Default:
''
- Example Values:
YourCustomHostname
- Max Length:
80
Hostname to use for the current message.
- Optional
- Type:
bool
- Default:
false
- Example Values:
true
Allows for the meta to be searchable in LogDNA.
- Optional
- Type:
string
- Default:
https://logs.logdna.com/logs/ingest
URL of the logging server.
- Optional
- Type:
string
- Default:
''
- Example Values:
10.0.0.1
IPv4 or IPv6 address for the current message.
- Optional
- Type:
string
- Default:
Info
- Example Values:
Debug
,Trace
,Info
,Warn
,Error
,Fatal
,YourCustomLevel
- Max Length:
80
Desired level for the current message.
- Optional
- Type:
string
- Default:
''
- Example Values:
c0:ff:ee:c0:ff:ee
MAC address for the current message.
- Optional
- Type:
int
- Default:
50
- Example Values:
10
Maximum total line lengths before a flush is forced.
- Optional
- Type:
string
Per-message meta data.
- Optional
- Type:
time.Duration
- Default:
30 * time.Second
- Example Values:
10
Time limit in seconds to wait before timing out.
- Optional
- Type:
string
- Default:
5
- Example Values:
logging,golang
Tags to be added for the current message.
- Optional
- Type:
time.Time
- Default Values:
time.Now()
- Example Values:
time.Now()
Epoch ms time to use for the current message.
- Required
- Type:
string
- Default:
''
Text of the log entry.
- Required
- Type:
string
- Default: ``
- Example Values:
Debug
,Trace
,Info
,Warn
,Error
,Fatal
,YourCustomLevel
- Max Length:
80
Desired level for the current message.
Close must be run when done with using a logger to forward any remaining buffered logs into the LogDNA product.
Copyright © LogDNA, released under an MIT license. See the LICENSE file and https://opensource.org/licenses/MIT
Happy Logging!