A Go package to handle logging for web services and CLI tools.
This package wraps
github.com/sirupsen/logrus
and adds defaults so that creating a sensible logger is as easy aslogger.New()
Github | https://github.com/usvc/go-logger |
Gitlab | https://gitlab.com/usvc/modules/go/logger |
import (
// ...
"github.com/usvc/go-logger"
// ...
)
log := logger.New()
log := logger.New(logger.Options{
Type: logger.TypeNoOp,
})
log := logger.NewLogrusEntry()
log := logger.New(logger.Options{
Output: logger.OutputStderr,
})
log := logger.New(logger.Options{
Output: logger.OutputStderr,
OutputFilePath: "./20200223.log",
})
var output bytes.Buffer
log := logger.New(logger.Options{
Output: logger.OutputCustom,
OutputStream: &output,
})
log := logger.New(logger.Options{
Format: logger.FormatJSON,
})
log := logger.New(logger.Options{
Level: logger.LevelDebug,
})
log := logger.New(logger.Options{
Fields: map[string]interface{}{
"module": "main",
},
})
log := logger.New(logger.Options{
Type: logger.TypeStdout,
})
Fields
map[string]interface{}
: Adds custom fields to the log entry.Format
logger.Format
: One ofFormatJSON
orFormatText
. Defaults toFormatText
.Level
logger.Level
: One ofLevelTrace
,LevelDebug
,LevelInfo
,LevelWarn
, orLevelError
. Defaults toLevelTrace
.Output
logger.Output
: One ofOutputCustom
,OutputFileSystem
,OutputStderr
, orOutputStdout
. Defaults toOutputStdout
.OutputFilePath
string
: Path to a log file, defaults to usingos.Stdout
if file cannot be created. Only applicable whenOutput
is set toOutputFileSystem
OutputStream
io.Writer
: Only applicable whenOutput
is set toOutputCustom
Type
logger.Type
: One ofTypeLevelled
orTypeStdout
. Defaults toTypeLevelled
.
Format defines how the logs output should be formatted.
FormatText
: output plain textFormatJSON
: output JSON-formatted text
Level defines the level of the logs.
LevelTrace
: all other logsLevelDebug
: logs related to code executionLevelInfo
: logs related to business-flow successLevelWarn
: logs related to business-flow errorsLevelError
: logs related to system-level failures
Output defines where the logs should be streamed to.
OutputCustom
: send logs to a customio.Writer
OutputFileSystem
: send logs to a fileOutputStderr
: send logs to standard errorOutputStdout
: send logs to standard output
Type defines the type of logger desired.
TypeLevelled
: defines a levelled loggerTypeNoOp
: defines a silent loggerTypeStdout
: defines a plaintext logger
The example application can be found at ./cmd/logger
. To try it out from this repository, run make run
.
To build it, run make build_production
.
- Clone this repository
- Run
make deps
to pull in external dependencies - Write some awesome stuff
- Run
make test
to ensure unit tests are passing - Push
To set up the CI pipeline in Gitlab:
- Run
make .ssh
- Copy the contents of the file generated at
./.ssh/id_rsa.base64
into an environment variable namedDEPLOY_KEY
in Settings > CI/CD > Variables - Navigate to the Deploy Keys section of the Settings > Repository > Deploy Keys and paste in the contents of the file generated at
./.ssh/id_rsa.pub
with the Write access allowed checkbox enabled
DEPLOY_KEY
: generate this by runningmake .ssh
and copying the contents of the file generated at./.ssh/id_rsa.base64
Code here is licensed under the MIT license by @zephinzer.