log

package
v0.0.0-...-67c52db Latest Latest
Warning

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

Go to latest
Published: May 20, 2022 License: MIT Imports: 5 Imported by: 0

README

logger

Go logging library.

Features

  • simplest Log interface for business-adapted log access.
  • std log implementation.
  • zerolog implementation.

Usage

std log
package log_test

import (
	"github.com/sraphs/go/log"
	"github.com/sraphs/go/log/stdlog"
)

func Example_stdlog() {
	stdlog.RegisterGlobal()

	log.Debug("hello world")

	// Outputs:
	// DEBUG: 2022/05/17 16:07:38.303205 example_test.go:21: hello world
}

zerolog
package log_test

import (
	"github.com/sraphs/go/log"
	"github.com/sraphs/go/log/zerolog"
)

func Example_zerolog() {
	zerolog.RegisterGlobal()

	log.Debug("hello world")

	// Outputs:
	// {"level":"debug","msg":"hello world","ts":"2022-05-17T15:58:44+08:00","caller":"example_test.go:11"}
}

Documentation

Overview

Package log provides a global logger for log.

Example (Stdlog)
package main

import (
	"os"

	"github.com/sraphs/go/log"
	"github.com/sraphs/go/log/stdlog"
)

func main() {
	log.Init(
		stdlog.New(os.Stdout, "", stdlog.LDefaultFlags).SetCallerDepth(log.CallerSkipFrameCount),
	)

	log.Debug("hello world")

	// Outputs:
	// DEBUG: 2022/05/19 10:36:33.528825 example_test.go:31: hello world
}
Output:

Example (Zerolog)
package main

import (
	"os"

	"github.com/sraphs/go/log"
	"github.com/sraphs/go/log/zerolog"
)

func main() {
	log.Init(
		zerolog.New(os.Stdout,
			zerolog.WithTimestamp(),
			zerolog.WithCallerWithSkipFrameCount(log.CallerSkipFrameCount),
			zerolog.WithStack(),
		),
	)

	log.Debug("hello world")

	// Outputs:
	// {"level":"debug","msg":"hello world","ts":"2022-05-19T10:36:18+08:00","caller":"example_test.go:20"}
}
Output:

Index

Examples

Constants

View Source
const CallerSkipFrameCount = 1

when use global Debug/Info ... skip 1 frames to get to the caller

Variables

This section is empty.

Functions

func Debug

func Debug(v ...interface{})

func Error

func Error(v ...interface{})

func Fatal

func Fatal(v ...interface{})

func GetLevel

func GetLevel() logger.Level

GetLevel returns the current global log level.

func GetLogger

func GetLogger() logger.Logger

GetLogger returns the current global logger.

func Info

func Info(v ...interface{})

func Init

func Init(l logger.Logger) logger.Logger

func IsLevelEnabled

func IsLevelEnabled(lvl logger.Level) bool

func Panic

func Panic(v ...interface{})

func SetLevel

func SetLevel(l logger.Level)

SetLevel sets the current global log level.

func SetLogger

func SetLogger(l logger.Logger)

SetLogger sets the current global logger.

func Warn

func Warn(v ...interface{})

Types

This section is empty.

Directories

Path Synopsis
package logger defines as interface a writer may implement in order
package logger defines as interface a writer may implement in order
package zerolog implements a Logger interface using zerolog.
package zerolog implements a Logger interface using zerolog.

Jump to

Keyboard shortcuts

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