# README
Logger
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 |
Usage
Importing
import (
// ...
"github.com/usvc/go-logger"
// ...
)
Instantiating a basic logger
log := logger.New()
Instantiating a logger without output
log := logger.New(logger.Options{
Type: logger.TypeNoOp,
})
Instantiating a basic logger instance
log := logger.NewLogrusEntry()
Logging to stderr
log := logger.New(logger.Options{
Output: logger.OutputStderr,
})
Logging to file system
log := logger.New(logger.Options{
Output: logger.OutputStderr,
OutputFilePath: "./20200223.log",
})
Logging to a buffer or custom io.Writer
var output bytes.Buffer
log := logger.New(logger.Options{
Output: logger.OutputCustom,
OutputStream: &output,
})
Logging in JSON
log := logger.New(logger.Options{
Format: logger.FormatJSON,
})
Logging only Debug level and above
log := logger.New(logger.Options{
Level: logger.LevelDebug,
})
Logging with a custom field
log := logger.New(logger.Options{
Fields: map[string]interface{}{
"module": "main",
},
})
Logging without levels
log := logger.New(logger.Options{
Type: logger.TypeStdout,
})
Documentation
Configuration
logger.Options
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
.
Constants
Format
Format defines how the logs output should be formatted.
FormatText
: output plain textFormatJSON
: output JSON-formatted text
Level
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
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
Type defines the type of logger desired.
TypeLevelled
: defines a levelled loggerTypeNoOp
: defines a silent loggerTypeStdout
: defines a plaintext logger
Example Application
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
.
Development Runbook
Getting Started
- 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
Continuous Integration (CI) Pipeline
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
License
Code here is licensed under the MIT license by @zephinzer.
# Packages
No description provided by the author
# Functions
No description provided by the author
No description provided by the author
No description provided by the author
# Constants
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
LevelDebug defines a level for logs that should appear in production logs but are not indicative of business flow success/failure.
LevelError defines a level of logs for events that are disruptive to the application and which may result in the application exitting.
LevelInfo defines a level for logs that are indicative of business flow success/failure.
LevelTrace defines a level for logs that only developers should see.
LevelWarn defines a level of logs for events that may disrupt flow and indicate that the code didn't execute perfectly as expected.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
# Variables
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
LevelvalueMap maps levels to numbers for loggers to determine whether a log should be displayed based on it's level.
No description provided by the author
LogrusLevelMap maps levels according to this package, to the github.com/sirupsen/logrus package's levels.
No description provided by the author
No description provided by the author
ValidLevels defines the levels available for use.
No description provided by the author
No description provided by the author
# Interfaces
No description provided by the author
# Type aliases
Format defines a valid log format.
Formats is a slice of Format.
Level is a type that represents a logical log level.
Levels is a slice of levels.
Output is a type of log output.
Outputs is a slice of Outputs.
Type represents a type of logger.
Types is a slice of Types.