logex

package module
v0.0.0-...-1e89730 Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2019 License: BSD-3-Clause Imports: 10 Imported by: 9

README

logex

A leveled log pkg for go. Very easy to use and fully customizes. Most importantly, it output goroutine-id in log line. output example:

NOTICE: 08-10 15:15:03.365: 3: basic_usage.go:25: note the third field of log line is the goroutine-id

Example

  • example.go:

    // The very basic usage of logex pkg
    package main
    
    import (
        "fmt"
        "os"
        "time"
    )
    
    import "github.com/Badangel/logex"
    
    func main() {
        if err := SetUpFileLogger("./log", "test", nil); err != nil {
            fmt.Fprintln(os.Stderr, "init file logger failed\n")
            os.Exit(1)
        }
        logex.SetLevel(logex.NOTICE) // set log level to NOTICE,
                                     // so DEBUG and TRACE logs won't show
                                     // in log file
    
        logex.Debug("this message won't show")
        logex.Trace("this message won't show neither")
        logex.Notice("hi, pi is:", 3.1415926)
        logex.Warning("this is warning message")
        logex.Fatal("all logs will output to os.Stderr by default")
    }
    

TODO

Documentation

Overview

Output log to files. By default it writes to xxx.log file and saves xxx.log to xxx.log.<date> and opens an new xxx.log file when an new day begins. And it will also remove log files 7 days ago. Log file name, output dir and log file lifetime can be customized.

Extended logging pkg for go. Output log format:

"LEVEL: DATE TIME: g=GOROUTINE_ID: FILE:LINE: LOG_CONTENT\n"

eg:

"NOTICE: 08-06 10:45:19.598: g=12: bvc.go:100: hello world"

Index

Constants

This section is empty.

Variables

View Source
var (
	LOGDIR_PERM  os.FileMode = 0755
	LOGFILE_PERM os.FileMode = 0644
	KEEP_SECONDS int         = 86400 * 7 // keeps old log for 86400*7 seconds(7 days)
)

Functions

func DailyChecker

func DailyChecker(now *time.Time, logfile *string) bool

DailyChecker returns true if now is the succeed day of daily.today

func Debug

func Debug(v ...interface{})

Debug is equivalent to Print() for DEBUG-level log.

func Debugf

func Debugf(format string, v ...interface{})

Debugf is equivalent to Printf() for DEBUG-level log.

func Fatal

func Fatal(v ...interface{})

Fatal is equivalent to Print() for FATAL-level log.

func Fatalf

func Fatalf(format string, v ...interface{})

Fatalf is equivalent to Printf() for FATAL-level log.

func Notice

func Notice(v ...interface{})

Notice is equivalent to Print() for NOTICE-level log.

func Noticef

func Noticef(format string, v ...interface{})

Noticef is equivalent to Printf() for NOTICE-level log.

func SetLevel

func SetLevel(level Level)

SetLevel sets the default logger level.

func SetOutput

func SetOutput(w io.Writer)

SetOutput sets the output destination for the standard logger.

func SetUpFileLogger

func SetUpFileLogger(logdir, prefix string, checker LogChecker) error

func Trace

func Trace(v ...interface{})

Trace is equivalent to Print() for TRACE-level log.

func Tracef

func Tracef(format string, v ...interface{})

Tracef is equivalent to Printf() for TRACE-level log.

func Warning

func Warning(v ...interface{})

Waring is equivalent to Print() for WARING-level log.

func Warningf

func Warningf(format string, v ...interface{})

Warningf is equivalent to Printf() for WARNING-level log.

Types

type FileLogger

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

type Level

type Level uint

Log Level type: FATAL > WARNING > NOTICE > TRACE > DEBUG

const (
	NONE Level = iota
	FATAL
	WARNING
	NOTICE
	TRACE
	DEBUG
	LEVEL_MAX
)

type LogChecker

type LogChecker func(now *time.Time, logfile *string) bool

LogChecker function returns true if need to close current log file and open an new one. now variable contains current time, and logfile contains absolute path of current log file.

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 New

func New(level Level, out io.Writer) *Logger

New creates a new Logger. The level variable sets the logger level. And the out variable sets the destination to which log data will be written.

func (*Logger) Output

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

Output writes the output for a logging event. The level variable indicates the output message level. Note, only message level greater than or equals to logger level can be written. Calldepth is used to recover the PC and is provided for generality, although at the moment on all pre-defined paths it will be 2. The string s contains the text to print. A newline is appended if the last character of s is not already a newline.

Directories

Path Synopsis
The very basic usage of logex pkg The very basic usage of logex pkg
The very basic usage of logex pkg The very basic usage of logex pkg

Jump to

Keyboard shortcuts

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