logger

package module
v0.0.0-...-3be76b2 Latest Latest
Warning

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

Go to latest
Published: Aug 30, 2023 License: MIT Imports: 3 Imported by: 1

README

Logger

ButterflyGate's standard logger

Language

Usage

  • simple usage
import (
	"github.com/ButterflyGate/logger"
	"github.com/ButterflyGate/logger/levels"
)

func main(){
	// definition and specify log level
	logger := logger.NewLogger(
		levels.Trace,
	)

	// output message
	logger.Info("hello,world\nhello\nhello")
}
Definition
  • use NewLogger.
  • Log level must be specified to use this logger at definition.
  • log levels are definition in logger/levels directory.
logger := logger.NewLogger(
	levels.Trace,
)
Options
  • You can customize the output.
  • The function NewLogger can take Optional Arguments controlling output format.
outputOption := logger.DefaultOutputOption().HideCursor().HideLevel().HideTimestamp()
formatOption := logger.DefaultFormatOption().FormatMessageRowLimit(0)
l := logger.NewLogger(
	levels.Trace,
	outputOption, formatOption,
)
Output Option
  • OutputOption can control these elements for output or hide
    • log level
    • timestamp
    • called cursor
    • message (change source)
    • struct data (change source)
    • struct name (change source)
option := logger.DefaultOutputOption().HideLevel().HideTimestamp().HideCursor()
l := logger.NewLogger(
	levels.Trace,
	option,
)
Format Option
  • FormatOption can control output format
    • using indent or not (json format only)
    • limit message rows count (string type log only)
    • json or text (not implement yet)
    • custom format (not implement yet)
option := logger.DefaultFormatOption().FormatMessageRowLimit(1)
l := logger.NewLogger(
	levels.Trace,
	option,
)

Sample Output

string case
  • usage
logger.Info("hello\nworld")
  • output
{
  "level": "Trace",
  "timestamp": "2022-10-15T13:35:07.987882308+09:00",
  "cursor": "~/go/src/logger/test/log_test.go:14",
  "message": [
    "hello",
    "world"
  ]
}
structure case
  • usage
yourStruct := something.NewYourStruct()
l.Notice(yourStruct)
  • output
{
  "level": "Notice",
  "timestamp": "2022-10-15T14:00:16.889360303+09:00",
  "cursor": "/go/src/logger/test/log_test.go:28",
  "structure_data": {
    "ID": 10,
    "FullName": "kyota tahsiro",
    "Birth": "2022-10-15T14:00:15.527879915+09:00"
  },
  "structure_name": "unknown"
}
  • it takes second argument for structure_name
  • usage
yourStruct := something.NewYourStruct()
l.Notice(yourStruct,"yourStruct")
  • output
{
  "level": "Notice",
  "timestamp": "2022-10-15T14:00:16.889360303+09:00",
  "cursor": "/go/src/logger/test/log_test.go:28",
  "structure_data": {
    "ID": 10,
    "FullName": "kyota tahsiro",
    "Birth": "2022-10-15T14:00:15.527879915+09:00"
  },
  "structure_name": "yourStruct"
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultFormatOption

func DefaultFormatOption() formatOption

func DefaultOutputOption

func DefaultOutputOption() outputOption

Types

type Logger

type Logger interface {
	ResetLevel(level levels.LogLevel)
	// contains filtered or unexported methods
}

func NewLogger

func NewLogger[T levels.LogLevelType](level T, options ...option) Logger

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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