log

package module
v0.0.2-beta Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 5, 2022 License: MIT Imports: 13 Imported by: 0

README

A package for structured logging

GitHub tag (latest SemVer pre-release) GitHub go.mod Go version GitHub Workflow Status Go Report Card PkgGoDev

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"
    log "github.com/2pisoftware/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.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Debug

func Debug(fields Fields) error

Debug calls Debug for the initialized Logger.

func Error

func Error(fields Fields) error

Error calls Error for the initialized Logger.

func Info

func Info(fields Fields) error

Info calls Info for the initialized Logger.

Types

type CloudWatchLogEventSlice

type CloudWatchLogEventSlice struct {
	// contains filtered or unexported fields
}

CloudWatchLogEventSlice stores a thread safe slice of InputLogEvents. size is the current size in bytes of the log messages taking into account the inputLogEventOffet.

type CloudWatchLogger

type CloudWatchLogger struct {
	// contains filtered or unexported fields
}

CloudWatchLogger is a structured logger that logs to CloudWatch and is thread safe.

func NewCloudWatchLogger

func NewCloudWatchLogger(ctx context.Context, profile string, region string, logGroupName string, globalFields Fields) (*CloudWatchLogger, error)

NewCloudWatchLogger initializes a new CloudWatchLogger object and returns it. The profile and region parameters are optional if authentication with CloudWatch can be provided in other ways, such as IAM roles. logGroupName is the name of the log group in CloudWatch. globalFields are the fields that are written in every log message.

func (*CloudWatchLogger) Debug

func (c *CloudWatchLogger) Debug(fields Fields) error

Debug writes a log message at a debug level.

func (*CloudWatchLogger) Error

func (c *CloudWatchLogger) Error(fields Fields) error

Error writes a log message at an error level.

func (*CloudWatchLogger) Info

func (c *CloudWatchLogger) Info(fields Fields) error

Info writes a log message at an info level.

type Fields

type Fields map[string]interface{}

Fields is a map that is written in a log message.

type Logger

type Logger interface {
	Info(fields Fields) error
	Error(fields Fields) error
	Debug(fields Fields) error
}

Logger is a structured logger interface.

var Log Logger

Log is the package wide Logger. This can be used to have a Logger that is globally available within a codebase.

type StandardLogger

type StandardLogger struct {
	// contains filtered or unexported fields
}

StandardLogger is a structured logger that is thread safe.

func NewStandardLogger

func NewStandardLogger(output io.Writer, globalFields Fields) *StandardLogger

NewStandardLogger initializes a new StandardLogger object and returns it. output is the output that the log messages are written to. globalFields are the fields that are written in every log message.

func (*StandardLogger) Debug

func (l *StandardLogger) Debug(fields Fields) error

Debug writes a log message at a debug level.

func (*StandardLogger) Error

func (l *StandardLogger) Error(fields Fields) error

Error writes a log message at an error level.

func (*StandardLogger) Info

func (l *StandardLogger) Info(fields Fields) error

Info writes a log message at an info level.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL