log

package module
v0.0.0-...-2db592d Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2019 License: MIT Imports: 10 Imported by: 0

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"
// }

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Debug

func Debug(v ...interface{})

Debug logs a message at level Debug on the default logger.

func Error

func Error(err error)

Error logs a message at level Error on the default logger.

func Info

func Info(v ...interface{})

Info logs a message at level Info on the default logger.

func SetFlattenMetadata

func SetFlattenMetadata(b bool)

SetFlattenMetadata sets the flag if metadata is going to be flattened. If the flag is put on, metadata is going to be flattened in output

func SetFormat

func SetFormat(fm formatter)

SetFormat sets the format of message output to the default logger.

func SetMetadata

func SetMetadata(md map[string]interface{})

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

func SetMinLevel

func SetMinLevel(lv level.Level)

SetMinLevel sets minumum logging level to the default logger.

func SetOutput

func SetOutput(out io.Writer)

SetOutput sets io.Writer as destination of logging message to the default logger.

func SetStdLogger

func SetStdLogger(lg *log.Logger)

SetStdLogger sets StdLogger that is used output message to the default logger.

func Warn

func Warn(v ...interface{})

Warn logs a message at level Warn on the default logger.

Types

type Logger

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

Logger has fields that Option or setter SetXXXX set.

func New

func New(ops ...Option) *Logger

New initialize new Logger with options.

func (*Logger) Debug

func (l *Logger) Debug(v ...interface{})

Debug logs a message at level Debug.

func (*Logger) Debugf

func (l *Logger) Debugf(format string, v ...interface{})

Debugf logs a formatted message at level Debug.

func (*Logger) Error

func (l *Logger) Error(err error)

Error logs a message at level Error.

func (*Logger) Info

func (l *Logger) Info(v ...interface{})

Info logs a message at level Info.

func (*Logger) Infof

func (l *Logger) Infof(format string, v ...interface{})

Infof logs a formatted message at level Info.

func (*Logger) SetMetadata

func (l *Logger) SetMetadata(meta map[string]interface{})

SetMetadata sets a metadata to a logger.

func (*Logger) Warn

func (l *Logger) Warn(v ...interface{})

Warn logs a message at level Warn.

func (*Logger) Warnf

func (l *Logger) Warnf(format string, v ...interface{})

Warn logs a formatted message at level Warn.

type NopLogger

type NopLogger struct{}

NopLogger just implements the below simple interface. ```

type Logger interface {
    Debug(v ...interface{})
    Info(v ...interface{})
    Warn(v ...interface{})
    Error(err error)
}

```

In writting tests, if you use Logger and you don't want any output, NopLogger can be used as stabs.

func (NopLogger) Debug

func (NopLogger) Debug(v ...interface{})

Debug do nothing.

func (NopLogger) Error

func (NopLogger) Error(err error)

Error do nothing.

func (NopLogger) Info

func (NopLogger) Info(v ...interface{})

Info do nothing.

func (NopLogger) Warn

func (NopLogger) Warn(v ...interface{})

Warn do nothing.

type Option

type Option func(Logger) Logger

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

func FlattenMetadata

func FlattenMetadata(b bool) Option

FlattenMetadata returns Option that sets the flag if metadata is going to be flattened. If the flag is put on, metadata is going to be flattened in output.

func Format

func Format(fm formatter) Option

Format returns Option that sets the format of message output to a new logger.

func Metadata

func Metadata(md map[string]interface{}) Option

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

func MinLevel

func MinLevel(lv level.Level) Option

MinLevel returns Option that sets minumum logging level to a new logger.

func Output

func Output(out io.Writer) Option

Output returns Option that sets io.Writer as the destination of logging message to a new logger.

Example
SetMinLevel(level.Debug)
SetFormat(format.JSONPretty)
SetMetadata(map[string]interface{}{
	"uesr_id":    "86f32b8b-ec0d-479f-aed1-1070aa54cecf",
	"request_id": "943ad105-7543-11e6-a9ac-65e093327849",
})
SetFlattenMetadata(true)
SetOutput(os.Stdout)

defaultLogger.nowFunc = func() time.Time { return time.Time{} }
defaultLogger.withoutTrace = true

Debug("debug")
Info("info")
Warn("warn")
Error(errors.New("error"))
Output:


{
  "level": "DEBUG",
  "message": "debug",
  "request_id": "943ad105-7543-11e6-a9ac-65e093327849",
  "time": "0001-01-01T00:00:00Z",
  "uesr_id": "86f32b8b-ec0d-479f-aed1-1070aa54cecf"
}
{
  "level": "INFO",
  "message": "info",
  "request_id": "943ad105-7543-11e6-a9ac-65e093327849",
  "time": "0001-01-01T00:00:00Z",
  "uesr_id": "86f32b8b-ec0d-479f-aed1-1070aa54cecf"
}
{
  "level": "WARN",
  "message": "warn",
  "request_id": "943ad105-7543-11e6-a9ac-65e093327849",
  "time": "0001-01-01T00:00:00Z",
  "uesr_id": "86f32b8b-ec0d-479f-aed1-1070aa54cecf"
}
{
  "error": "*errors.errorString: error",
  "level": "ERROR",
  "request_id": "943ad105-7543-11e6-a9ac-65e093327849",
  "time": "0001-01-01T00:00:00Z",
  "uesr_id": "86f32b8b-ec0d-479f-aed1-1070aa54cecf"
}

func StdLogger

func StdLogger(lg *log.Logger) Option

StdLogger returns Option that sets StdLogger that is used output message to a new logger.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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