logger

package module
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2022 License: MIT Imports: 11 Imported by: 3

README

Logger for Go

Install

go get -u github.com/fitv/go-logger

Usage

package main

import (
    "github.com/fitv/go-logger"
)

func main() {
    fileWriter := logger.NewFileWriter(&logger.Option{
        Path:  "/app/web.log",
        Daily: true,
        Days:  7,
    })
    defer fileWriter.Close()

    log := logger.New()
    log.SetOutput(fileWriter)
    log.SetLevel(logger.DebugLevel)

    log.Debug("debug")
    log.Info("info")
    log.Warn("warn")
    log.Error("error")
    log.Panic("panic")
    log.Fatal("fatal")
}

Documentation

Index

Constants

View Source
const (
	DebugLevel = Level(iota)
	InfoLevel
	WarnLevel
	ErrorLevel
	PanicLevel
	FatalLevel
)

Variables

View Source
var LevelMap = map[Level]string{
	DebugLevel: "debug",
	InfoLevel:  "info",
	WarnLevel:  "warn",
	ErrorLevel: "error",
	PanicLevel: "panic",
	FatalLevel: "fatal",
}

LevelMap maps a level to a string.

Functions

This section is empty.

Types

type FileWriter added in v1.0.2

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

func NewFileWriter added in v1.0.2

func NewFileWriter(opt *Option) *FileWriter

NewFileWriter creates a new FileWriter.

func (*FileWriter) Close added in v1.0.2

func (l *FileWriter) Close() error

Close closes the logger.

func (*FileWriter) Write added in v1.0.2

func (l *FileWriter) Write(p []byte) (n int, err error)

Write writes the log message.

type Level

type Level int

func (Level) String

func (l Level) String() string

String returns the string representation of the level.

type Logger

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

func New

func New() *Logger

New creates a new Logger.

func (*Logger) Debug

func (l *Logger) Debug(args ...any)

Debug writes a message to the log using the DEBUG level.

func (*Logger) Error

func (l *Logger) Error(args ...any)

Error writes a message to the log using the ERROR level.

func (*Logger) Fatal

func (l *Logger) Fatal(args ...any)

Fatal writes a message to the log using the FATAL level. The process will exit with status set to 1.

func (*Logger) Info

func (l *Logger) Info(args ...any)

Info writes a message to the log using the INFO level.

func (*Logger) Log

func (l *Logger) Log(level Level, args ...any)

Log writes a message to the log using the given level.

func (*Logger) Panic added in v1.0.3

func (l *Logger) Panic(args ...any)

Panic writes a message to the log using the PANIC level. The process will panic after writing the message.

func (*Logger) SetLevel

func (l *Logger) SetLevel(level Level)

SetLevel sets the logger level.

func (*Logger) SetOutput added in v1.0.3

func (l *Logger) SetOutput(out io.Writer)

SetOutput sets the output writer.

func (*Logger) SetTimeLayout

func (l *Logger) SetTimeLayout(layout string)

SetTimeLayout sets the time layout.

func (*Logger) Warn

func (l *Logger) Warn(args ...any)

Warn writes a message to the log using the WARN level.

func (*Logger) Write added in v1.0.2

func (l *Logger) Write(p []byte) (n int, err error)

type Option

type Option struct {
	// Path of the log file. eg: /logs/app.log
	Path string

	// Whether to create a new file every day.
	Daily bool

	// Days is the number of days to keep log files.
	Days int
}

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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