jlo

package module
v1.0.1-0...-236f79e Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2021 License: MIT Imports: 10 Imported by: 1

README

jlo

Light-weight json logging in go

  • Default log level: INFO
  • Minimum go version/dependencies: See go.mod
  • Release versioning: Semantic versioning/MAJOR.MINOR.PATCH
  • Suggestions/problems: Please create an issue

Usage


l := jlo.NewLogger(os.Stdout)
l.Infof("I'm real")

l.SetLogLevel(jlo.DebugLevel)
l.Debugf("what you get is what you %s", "see")

l = l.WithField("@request_id", "aa33ee55")
l.Errorf("What you tryna to do to me?")

Example output

{"@timestamp": "2018-08-17T13:24:08.856339554Z","@level":"info","@message": "I'm real"}
{"@timestamp": "2018-08-17T13:24:08.856339554Z","@level":"debug","@message": "what you get is what you see"}
{"@timestamp": "2018-08-17T13:24:08.856391733Z","@level":"error","@request_id":"aa33ee55","@message": "What you tryna to do to me?"}
...

Maintainers:

Documentation

Overview

Light-weight json logging

Index

Examples

Constants

View Source
const (
	// FieldKeyLevel is the log level log field name
	FieldKeyLevel = "@level"
	// FieldKeyMsg is the log message log field name
	FieldKeyMsg = "@message"
	// FieldKeyTime is the time log field name
	FieldKeyTime = "@timestamp"
	// FieldKeyCommit is the commit log field name
	FieldKeyCommit = "@commit"
)

Variables

View Source
var Now = func() time.Time {
	return time.Now().UTC()
}

Functions

func SetLogLevel

func SetLogLevel(level LogLevel)

SetLogLevel changes the global log level

Types

type Entry

type Entry map[string]interface{}

func (Entry) MarshalEasyJSON

func (v Entry) MarshalEasyJSON(w *jwriter.Writer)

MarshalEasyJSON supports easyjson.Marshaler interface

func (*Entry) UnmarshalEasyJSON

func (v *Entry) UnmarshalEasyJSON(l *jlexer.Lexer)

UnmarshalEasyJSON supports easyjson.Unmarshaler interface

type LogLevel

type LogLevel int

LogLevel represents a log level used by Logger type

const (
	// UnknownLevel means the log level could not be parsed
	UnknownLevel LogLevel = iota
	// DebugLevel is the most verbose output and logs messages on all levels
	DebugLevel
	// InfoLevel logs messages on all levels except the DebugLevel
	InfoLevel
	// WarningLevel logs messages on all levels except DebugLevel and InfoLevel
	WarningLevel
	// ErrorLevel logs messages on ErrorLevel and FatalLevel
	ErrorLevel
	// FatalLevel logs messages on FatalLevel
	FatalLevel
)

func (LogLevel) MarshalEasyJSON

func (lvl LogLevel) MarshalEasyJSON() ([]byte, error)

func (LogLevel) String

func (l LogLevel) String() string

String returns a string representation of the log level

Example
package main

import (
	"fmt"

	"github.com/dcmn-com/jlo"
)

func main() {
	level := jlo.DebugLevel.String()
	fmt.Println(level)
}
Output:

debug

type Logger

type Logger struct {
	FieldKeyMsg   string
	FieldKeyLevel string
	FieldKeyTime  string
	// contains filtered or unexported fields
}

Logger logs json formatted messages to a certain output destination

func DefaultLogger

func DefaultLogger() *Logger

DefaultLogger returns a new default logger logging to stdout

func NewLogger

func NewLogger(out io.Writer) *Logger

NewLogger creates a new logger which will write to the passed in io.Writer

Example
package main

import (
	"os"

	"github.com/dcmn-com/jlo"
)

func main() {
	l := jlo.NewLogger(os.Stdout)
	l.FieldKeyLevel = "lvl"
	l.FieldKeyMsg = "msg"
	l.FieldKeyTime = "time"

	l.Infof("I'm real")
}
Output:

{"lvl":"info","msg":"I'm real","time":"2018-08-02T21:48:56.856339554Z"}
Example (CustomFields)
package main

import (
	"os"

	"github.com/dcmn-com/jlo"
)

func main() {
	l := jlo.NewLogger(os.Stdout)

	l.Infof("I'm real")
}
Output:

{"@level":"info","@message":"I'm real","@timestamp":"2018-08-02T21:48:56.856339554Z"}

func (*Logger) Debugf

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

Debugf logs a messages on DebugLevel

Example
package main

import (
	"os"

	"github.com/dcmn-com/jlo"
)

func main() {
	l := jlo.NewLogger(os.Stdout)

	l.SetLogLevel(jlo.DebugLevel)
	l.Debugf("I'm real")
}
Output:

{"@level":"debug","@message":"I'm real","@timestamp":"2018-08-02T21:48:56.856339554Z"}

func (*Logger) Errorf

func (l *Logger) Errorf(format string, args ...interface{})

Errorf logs a messages on ErrorLevel

Example
package main

import (
	"os"

	"github.com/dcmn-com/jlo"
)

func main() {
	l := jlo.NewLogger(os.Stdout)

	l.Errorf("I'm real")
}
Output:

{"@level":"error","@message":"I'm real","@timestamp":"2018-08-02T21:48:56.856339554Z"}

func (*Logger) Fatalf

func (l *Logger) Fatalf(format string, args ...interface{})

Fatalf logs a message on FatalLevel

Example
package main

import (
	"os"

	"github.com/dcmn-com/jlo"
)

func main() {
	l := jlo.NewLogger(os.Stdout)

	l.Fatalf("I'm real")
}
Output:

{"@level":"fatal","@message":"I'm real","@timestamp":"2018-08-02T21:48:56.856339554Z"}

func (*Logger) Infof

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

Infof logs a messages on InfoLevel

Example
package main

import (
	"os"

	"github.com/dcmn-com/jlo"
)

func main() {
	l := jlo.NewLogger(os.Stdout)

	l.Infof("I'm real")
}
Output:

{"@level":"info","@message":"I'm real","@timestamp":"2018-08-02T21:48:56.856339554Z"}

func (*Logger) SetLogLevel

func (l *Logger) SetLogLevel(level LogLevel)

SetLogLevel changes the log level

Example
package main

import (
	"os"

	"github.com/dcmn-com/jlo"
)

func main() {
	l := jlo.NewLogger(os.Stdout)

	l.SetLogLevel(jlo.DebugLevel)
	l.Debugf("I'm real")
}
Output:

{"@level":"debug","@message":"I'm real","@timestamp":"2018-08-02T21:48:56.856339554Z"}

func (*Logger) Warnf

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

Warnf logs a messages on WarningLevel

Example
package main

import (
	"os"

	"github.com/dcmn-com/jlo"
)

func main() {
	l := jlo.NewLogger(os.Stdout)

	l.Warnf("I'm real")
}
Output:

{"@level":"warning","@message":"I'm real","@timestamp":"2018-08-02T21:48:56.856339554Z"}

func (*Logger) WithField

func (l *Logger) WithField(key string, value interface{}) *Logger

WithField returns a copy of the logger with a custom field set, which will be included in all subsequent logs

Example
package main

import (
	"os"

	"github.com/dcmn-com/jlo"
)

func main() {
	l := jlo.NewLogger(os.Stdout)

	l = l.WithField("@request_id", "aa33ee55")
	l.Infof("I'm real")
}
Output:

{"@level":"info","@message":"I'm real","@request_id":"aa33ee55","@timestamp":"2018-08-02T21:48:56.856339554Z"}

Jump to

Keyboard shortcuts

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