Skip to content
/ go-logger Public

A Go package to handle logging for web services and CLI tools

License

Notifications You must be signed in to change notification settings

usvc/go-logger

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

44 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Logger

latest release build status pipeline status test coverage maintainability

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 as logger.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 of FormatJSON or FormatText. Defaults to FormatText.
  • Level logger.Level: One of LevelTrace, LevelDebug, LevelInfo, LevelWarn, or LevelError. Defaults to LevelTrace.
  • Output logger.Output: One of OutputCustom, OutputFileSystem, OutputStderr, or OutputStdout. Defaults to OutputStdout.
  • OutputFilePath string: Path to a log file, defaults to using os.Stdout if file cannot be created. Only applicable when Output is set to OutputFileSystem
  • OutputStream io.Writer: Only applicable when Output is set to OutputCustom
  • Type logger.Type: One of TypeLevelled or TypeStdout. Defaults to TypeLevelled.

Constants

Format

Format defines how the logs output should be formatted.

  • FormatText: output plain text
  • FormatJSON: output JSON-formatted text

Level

Level defines the level of the logs.

  • LevelTrace: all other logs
  • LevelDebug: logs related to code execution
  • LevelInfo: logs related to business-flow success
  • LevelWarn: logs related to business-flow errors
  • LevelError: logs related to system-level failures

Output

Output defines where the logs should be streamed to.

  • OutputCustom: send logs to a custom io.Writer
  • OutputFileSystem: send logs to a file
  • OutputStderr: send logs to standard error
  • OutputStdout: send logs to standard output

Type

Type defines the type of logger desired.

  • TypeLevelled: defines a levelled logger
  • TypeNoOp: defines a silent logger
  • TypeStdout: 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

  1. Clone this repository
  2. Run make deps to pull in external dependencies
  3. Write some awesome stuff
  4. Run make test to ensure unit tests are passing
  5. Push

Continuous Integration (CI) Pipeline

To set up the CI pipeline in Gitlab:

  1. Run make .ssh
  2. Copy the contents of the file generated at ./.ssh/id_rsa.base64 into an environment variable named DEPLOY_KEY in Settings > CI/CD > Variables
  3. 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 running make .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.