grlog

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Dec 18, 2023 License: MIT Imports: 10 Imported by: 6

README

grlog - A golang rotate file logging package

grlog is a simple log package that is extended from the log standard library.

Features

  • rotate file and timed rotate file
  • support asynchronous writing
  • log level

Use

download

go get -u github.com/shaopson/grlog

import

import "github.com/shaopson/grlog"
Example
package main

import "github.com/shaopson/grlog"

func main() {
    //shortcuts
    //default stderr writer
    grlog.Debug("debug")
    grlog.Info("info")
    grlog.Warn("warn")
    grlog.Error("error")
    grlog.SetLevel(grlog.LevelDebug)
    grlog.Log(grlog.LevelInfo, "hello")
}

Rotate file
//5 backup files,  default file size,  sync write mode
writer, err := grlog.NewRotateFile("test.log", 5, 0, false)
defer writer.Close()
if err != nil {
    panic(err)
}
log := grlog.Default()
log.SetOutput(writer)
log.Info("debug")

// timed rotate file,
writer, err := grlog.NewTimedRotateFile("test.log", 5, 0, false)
Async Write
writer, err := grlog.NewRotatingFile("test.log", 5, -1, true)
// don`t forget close!!!
defer writer.Close()

Documentation

Index

Constants

View Source
const (
	FlagDate   = 1 << iota // the date in the local time zone: 2009/01/23
	FlagTime               // the time in the local time zone: 01:23:23
	FlagMtime              // microsecond resolution: 01:23:23.123123.  assumes Ltime.
	FlagLFile              // full file name and line number: /a/b/c/d.go:23
	FlagSFile              // final file name element and line number: d.go:23. overrides Llongfile
	FlagUTC                // if Ldate or Ltime is set, use UTC rather than the local time zone
	FlagPrefix             // move the "prefix" from the beginning of the line to before the message
	FlagLevel
	FlagStd = FlagDate | FlagTime | FlagLevel // initial values for the standard logger

	LevelError = iota
	LevelWarn
	LevelInfo
	LevelDebug
)

Variables

This section is empty.

Functions

func Debug

func Debug(format string, v ...any)

func Error

func Error(format string, v ...any)

func Fatal added in v0.1.1

func Fatal(v ...any)

Fatal is equivalent to Print() followed by a call to os.Exit(1).

func Fatalf

func Fatalf(format string, v ...any)

Fatalf is equivalent to Printf() followed by a call to os.Exit(1).

func Fatalln added in v0.1.2

func Fatalln(v ...any)

Fatalln is equivalent to Println() followed by a call to os.Exit(1).

func Flags

func Flags() int

Flags returns the output flags for the standard logger. The flag bits are Ldate, Ltime, and so on.

func Info

func Info(format string, v ...any)

func Level

func Level() int

func LevelString added in v0.1.2

func LevelString() string

func Log added in v0.1.2

func Log(level int, format string, v ...any)

func Output added in v0.1.2

func Output(calldepth int, s string) error

Output writes the output for a logging event. The string s contains the text to print after the prefix specified by the flags of the Logger. A newline is appended if the last character of s is not already a newline. Calldepth is the count of the number of frames to skip when computing the file name and line number if Llongfile or Lshortfile is set; a value of 1 will print the details for the caller of Output.

func Panic added in v0.1.1

func Panic(v ...any)

Panic is equivalent to Print() followed by a call to panic().

func Panicf

func Panicf(format string, v ...any)

Panicf is equivalent to Printf() followed by a call to panic().

func Panicln added in v0.1.2

func Panicln(v ...any)

Panicln is equivalent to Println() followed by a call to panic().

func Prefix added in v0.1.2

func Prefix() string

Prefix returns the output prefix for the standard logger.

func Print added in v0.1.2

func Print(v ...any)

Print calls Output to print to the standard logger. Arguments are handled in the manner of fmt.Print.

func Printf added in v0.1.2

func Printf(format string, v ...any)

Printf calls Output to print to the standard logger. Arguments are handled in the manner of fmt.Printf.

func Println added in v0.1.2

func Println(v ...any)

Println calls Output to print to the standard logger. Arguments are handled in the manner of fmt.Println.

func SetFlags

func SetFlags(flag int)

SetFlags sets the output flags for the standard logger. The flag bits are Ldate, Ltime, and so on.

func SetLevel added in v0.1.1

func SetLevel(level int)

func SetOutput

func SetOutput(w io.Writer)

SetOutput sets the output destination for the standard logger.

func SetPrefix added in v0.1.2

func SetPrefix(prefix string)

SetPrefix sets the output prefix for the standard logger.

func Warn

func Warn(format string, v ...any)

func Writer added in v0.1.2

func Writer() io.Writer

Writer returns the output destination for the standard logger.

Types

type Logger

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

A Logger represents an active logging object that generates lines of output to an io.Writer. Each logging operation makes a single call to the Writer's Write method. A Logger can be used simultaneously from multiple goroutines; it guarantees to serialize access to the Writer.

func Default

func Default() *Logger

Default returns the standard logger used by the package-level output functions.

func New

func New(out io.Writer, prefix string, flag int, level int) *Logger

New creates a new Logger

func (*Logger) Debug

func (l *Logger) Debug(format string, v ...any)

func (*Logger) Error

func (l *Logger) Error(format string, v ...any)

func (*Logger) Fatal added in v0.1.1

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

Fatal is equivalent to l.Print() followed by a call to os.Exit(1).

func (*Logger) Fatalf

func (l *Logger) Fatalf(format string, v ...any)

Fatalf is equivalent to l.Printf() followed by a call to os.Exit(1).

func (*Logger) Fatalln added in v0.1.2

func (l *Logger) Fatalln(v ...any)

Fatalln is equivalent to l.Println() followed by a call to os.Exit(1).

func (*Logger) Flags

func (l *Logger) Flags() int

Flags returns the output flags for the logger. The flag bits are Ldate, Ltime, and so on.

func (*Logger) Info

func (l *Logger) Info(format string, v ...any)

func (*Logger) Level added in v0.1.1

func (l *Logger) Level() int

func (*Logger) LevelString added in v0.1.2

func (l *Logger) LevelString() string

func (*Logger) Log added in v0.1.2

func (l *Logger) Log(level int, format string, v ...any)

func (*Logger) Output added in v0.1.2

func (l *Logger) Output(calldepth int, s string, level ...int) error

func (*Logger) Panic added in v0.1.1

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

Panic is equivalent to l.Print() followed by a call to panic().

func (*Logger) Panicf

func (l *Logger) Panicf(format string, v ...any)

Panicf is equivalent to l.Printf() followed by a call to panic().

func (*Logger) Panicln added in v0.1.2

func (l *Logger) Panicln(v ...any)

Panicln is equivalent to l.Println() followed by a call to panic().

func (*Logger) Prefix added in v0.1.2

func (l *Logger) Prefix() string

Prefix returns the output prefix for the logger.

func (*Logger) Print added in v0.1.2

func (l *Logger) Print(v ...any)

Print calls l.Output to print to the logger. Arguments are handled in the manner of fmt.Print.

func (*Logger) Printf added in v0.1.2

func (l *Logger) Printf(format string, v ...any)

Printf calls l.Output to print to the logger. Arguments are handled in the manner of fmt.Printf.

func (*Logger) Println added in v0.1.2

func (l *Logger) Println(v ...any)

Println calls l.Output to print to the logger. Arguments are handled in the manner of fmt.Println.

func (*Logger) SetFlags

func (l *Logger) SetFlags(flag int)

SetFlags sets the output flags for the logger. The flag bits are Ldate, Ltime, and so on.

func (*Logger) SetLevel added in v0.1.1

func (l *Logger) SetLevel(level int)

func (*Logger) SetOutput

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

SetOutput sets the output destination for the logger.

func (*Logger) SetPrefix added in v0.1.2

func (l *Logger) SetPrefix(prefix string)

SetPrefix sets the output prefix for the logger.

func (*Logger) Warn

func (l *Logger) Warn(format string, v ...any)

func (*Logger) Writer added in v0.1.2

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

Writer returns the output destination for the logger.

type RotateFile added in v0.1.2

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

func NewRotateFile added in v0.1.2

func NewRotateFile(fileName string, backupCount int, fileSize int64, async bool) (*RotateFile, error)

fileName: log file path: a/b/c.log backupCount: backup files, if backupCount=3: a.log a.log-2023-12-01 a.log-2023-12-02 a.log-2023-12-03 fileSize: log file max size, default size 16m async: asynchronous write

func (*RotateFile) Close added in v0.1.2

func (self *RotateFile) Close() error

func (*RotateFile) IsAsync added in v0.1.2

func (self *RotateFile) IsAsync() bool

func (*RotateFile) Write added in v0.1.2

func (self *RotateFile) Write(p []byte) (n int, err error)

type TimedRotateFile added in v0.1.2

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

func NewTimedRotateFile added in v0.1.2

func NewTimedRotateFile(fileName string, backupCount int, fileSize int64, async bool) (*TimedRotateFile, error)

backup yesterday's files at 00:00 every day fileName: log file path: a/b/c.log backupCount: backup files, if backupCount=3: a.log a.log-2023-12-01 a.log-2023-12-02 a.log-2023-12-03 fileSize: log file max size, default size 16m async: asynchronous write

func (*TimedRotateFile) Close added in v0.1.2

func (self *TimedRotateFile) Close() error

func (*TimedRotateFile) IsAsync added in v0.1.2

func (self *TimedRotateFile) IsAsync() bool

func (*TimedRotateFile) Write added in v0.1.2

func (self *TimedRotateFile) Write(p []byte) (n int, err error)

Jump to

Keyboard shortcuts

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