Skip to content

2pisoftware/gofor-little-log

 
 

Repository files navigation

A package for structured logging

GitHub tag (latest SemVer pre-release) GitHub go.mod Go version License: MIT 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.