Categorygithub.com/kyfk/log
modulepackage
0.0.0-20191111221404-2db592d59ca3
Repository: https://github.com/kyfk/log.git
Documentation: pkg.go.dev

# README

Log

GoDoc Build Status Go Report Card codecov codebeat badge

This is the simplest logging for Go.

Level Management

Use the level package and MinLevel/SetMinLevel.

// Set minimum level Warn to default logger.
log.SetMinLevel(level.Warn)

log.Debug("debug") // Output nothing
log.Warn("warn") // Output `warn`

Output Format Customization

You can customize the output format easily. Let's use Format/SetFormat to set the output format into the logger.

SetFormat(format.JSON)
logger.Info("info")
// Output:
// {"level":"INFO","message":"info","time":"2019-10-22T16:50:17.637733482+09:00"}

SetFormat(format.JSONPretty)
logger.Info("info")
// Output:
// {
//   "level": "INFO",
//   "message": "info",
//   "time": "2019-10-22T16:49:00.253014475+09:00"
// }

This repository supports only 2 formats that are plain JSON and pretty JSON.

however, you can make a new format that is along func(map[string]interface{}) string. After creating it, just needed to use Format/SetFormat to set it into the logger.

Common Output Field (Metadata)

If you use some querying service for searching specific logs like BigQuery, CloudWatch Logs Insight, Elasticsearch and other more, Metadata/SetMetadata can be used to set additional pieces of information to be able to search conveniently. For instance, Service ID, HTTP Request ID, the id of user signed in, EC2 instance-id and other more.

logger := log.New(
    log.Metadata(map[string]interface{}{
        "uesr_id":    "86f32b8b-ec0d-479f-aed1-1070aa54cecf",
        "request_id": "943ad105-7543-11e6-a9ac-65e093327849",
    }),
    log.FlattenMetadata(true),
    log.Format(format.JSONPretty),
)

logger.Info("info")
// Output:
// {
//   "level": "INFO",
//   "message": "info",
//   "request_id": "943ad105-7543-11e6-a9ac-65e093327849",
//   "time": "2019-10-22T17:02:02.748123389+09:00",
//   "uesr_id": "86f32b8b-ec0d-479f-aed1-1070aa54cecf"
// }

Example

package main

import (
    "errors"

    "github.com/kyfk/log"
    "github.com/kyfk/log/format"
    "github.com/kyfk/log/level"
)

func main() {
    logger := log.New(
        log.MinLevel(level.Warn),
        log.Format(format.JSONPretty),
        log.Metadata(map[string]interface{}{
            "service":    "book",
            "uesr_id":    "86f32b8b-ec0d-479f-aed1-1070aa54cecf",
            "request_id": "943ad105-7543-11e6-a9ac-65e093327849",
            "path":       "/operator/hello",
            // more other metadatas
        }),
        log.FlattenMetadata(true),
    )

    logger.Debug("debug")
    logger.Info("info")
    logger.Warn("warn")
    logger.Error(errors.New("error"))
}

// Output:
//
// {
//   "level": "WARN",
//   "message": "warn",
//   "path": "/operator/hello",
//   "request_id": "943ad105-7543-11e6-a9ac-65e093327849",
//   "service": "book",
//   "time": "2019-10-22T16:28:04.180385072+09:00",
//   "trace": [
//     "main.main /home/koya/go/src/github.com/kyfk/log/example/main.go:26",
//     "runtime.main /home/koya/go/src/github.com/golang/go/src/runtime/proc.go:203",
//     "runtime.goexit /home/koya/go/src/github.com/golang/go/src/runtime/asm_amd64.s:1357"
//   ],
//   "uesr_id": "86f32b8b-ec0d-479f-aed1-1070aa54cecf"
// }
// {
//   "error": "*errors.fundamental: error",
//   "level": "ERROR",
//   "path": "/operator/hello",
//   "request_id": "943ad105-7543-11e6-a9ac-65e093327849",
//   "service": "book",
//   "time": "2019-10-22T16:28:04.180589439+09:00",
//   "trace": [
//     "main.main /home/koya/go/src/github.com/kyfk/log/example/main.go:27",
//     "runtime.main /home/koya/go/src/github.com/golang/go/src/runtime/proc.go:203",
//     "runtime.goexit /home/koya/go/src/github.com/golang/go/src/runtime/asm_amd64.s:1357"
//   ],
//   "uesr_id": "86f32b8b-ec0d-479f-aed1-1070aa54cecf"
// }

# Packages

No description provided by the author
No description provided by the author
No description provided by the author

# Functions

Debug logs a message at level Debug on the default logger.
Error logs a message at level Error on the default logger.
FlattenMetadata returns Option that sets the flag if metadata is going to be flattened.
Format returns Option that sets the format of message output to a new logger.
Info logs a message at level Info on the default logger.
Metadata sets metadata to a new logger.
MinLevel returns Option that sets minumum logging level to a new logger.
New initialize new Logger with options.
Output returns Option that sets io.Writer as the destination of logging message to a new logger.
SetFlattenMetadata sets the flag if metadata is going to be flattened.
SetFormat sets the format of message output to the default logger.
SetMetadata sets metadata to default logger.
SetMinLevel sets minumum logging level to the default logger.
SetOutput sets io.Writer as destination of logging message to the default logger.
SetStdLogger sets StdLogger that is used output message to the default logger.
StdLogger returns Option that sets StdLogger that is used output message to a new logger.
Warn logs a message at level Warn on the default logger.

# Structs

Logger has fields that Option or setter SetXXXX set.
NopLogger just implements the below simple interface.

# Type aliases

Option is a function for initialization in the constructor of Logger.