Coder Social home page Coder Social logo

gofor-little / log Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 2.0 227 KB

A structured logger that supports AWS CloudWatch.

Home Page: https://pkg.go.dev/github.com/gofor-little/log

License: MIT License

Go 100.00%
golang-package structured-logging aws-cloudwatch-logs golang aws aws-sdk-go

log's Introduction

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.