Categorygithub.com/usvc/go-logger
modulepackage
0.6.5
Repository: https://github.com/usvc/go-logger.git
Documentation: pkg.go.dev

# README

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()

Githubhttps://github.com/usvc/go-logger
Gitlabhttps://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.

# 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

# Structs

No description provided by the author
Options holds configuration for a logger instance.
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.