# README
A package for structured logging
Introduction
- Structured logging
- Supports AWS CloudWatch
- Simple interface for implementing your own
Example
package main
import (
"fmt"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/gofor-little/log"
)
func main() {
// Standard logger writes to a user defined io.Writer.
log.Log = log.NewStandardLogger(os.Stdout, log.Fields{
"tag": "standard_logger",
})
// CloudWatch logger writes to an AWS CloudWatch log group.
sess, err = session.NewSession()
log.Log, err = log.NewCloudWatchLogger(context.Background(), os.Getenv("AWS_PROFILE"), os.Getenv("AWS_REGION"), "CloudWatchLoggerTest", log.Fields{
"tag": "cloudWatchLoggerTest",
})
if err != nil {
panic(err)
}
// Log at info, error and debug levels.
log.Info(log.Fields{
"message": "info message",
"bool": true,
"int": 64,
"float": 3.14159,
})
log.Error(log.Fields{
"string": "error message",
"bool": true,
"int": 64,
"float": 3.14159,
})
log.Debug(log.Fields{
"string": "debug message",
"bool": true,
"int": 64,
"float": 3.14159,
})
}
Testing
Ensure the following environment variables are set, usually with a .env file.
AWS_PROFILE
(an AWS CLI profile name)AWS_REGION
(a valid AWS region)
Run go test -v -race ./...
in the root directory.
# Functions
Debug calls Debug for the initialized Logger.
Error calls Error for the initialized Logger.
Info calls Info for the initialized Logger.
NewCloudWatchLogger initializes a new CloudWatchLogger object and returns it.
NewStandardLogger initializes a new StandardLogger object and returns it.
# Variables
Log is the package wide Logger.
# Structs
CloudWatchLogEventSlice stores a thread safe slice of InputLogEvents.
CloudWatchLogger is a structured logger that logs to CloudWatch and is thread safe.
StandardLogger is a structured logger that is thread safe.
# Interfaces
Logger is a structured logger interface.
# Type aliases
Fields is a map that is written in a log message.