log

package module
v1.0.6 Latest Latest
Warning

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

Go to latest
Published: May 17, 2023 License: MIT Imports: 10 Imported by: 3

README

log

GoDoc Build Status codecov Go Report Card GitHub release LICENSE

Package log implements multilevel logging.

Feature

  • Custom prefix
  • Multiple levels
  • Highlight color
  • File line
  • Call stack

Level

  • All
  • Trace
  • Debug
  • Info
  • Notice
  • Warn
  • Error
  • Panic
  • Fatal
  • Off

Get started

Install
go get github.com/hslam/log
Import
import "github.com/hslam/log"
Usage
Example
package main

import (
	"github.com/hslam/log"
)

func main() {
	logger := log.New()
	logger.SetPrefix("LogPrefix")
	logger.SetLevel(log.AllLevel)
	logger.SetHighlight(true)
	logger.SetLine(true)

	logger.Assertf(1 == 1, "%d %s %t", 1024, "HelloWorld", true)

	logger.Allf("%d %s %t", 1024, "HelloWorld", true)
	logger.Tracef("%d %s %t", 1024, "HelloWorld", true)
	logger.Debugf("%d %s %t", 1024, "HelloWorld", true)
	logger.Infof("%d %s %t", 1024, "HelloWorld", true)
	logger.Noticef("%d %s %t", 1024, "HelloWorld", true)
	logger.Warnf("%d %s %t", 1024, "HelloWorld", true)
	logger.Errorf("%d %s %t", 1024, "HelloWorld", true)
	defer func() {
		recover()
		logger.Fatalf("%d %s %t", 1024, "HelloWorld", true)
	}()
	logger.Panicf("%d %s %t", 1024, "HelloWorld", true)
}
Output
[LogPrefix] [2023/05/13 18:21:51.183 +08:00] [ALL] [main.go:16] ["1024 HelloWorld true"]
[LogPrefix] [2023/05/13 18:21:51.183 +08:00] [TRACE] [main.go:17] ["1024 HelloWorld true"]
[LogPrefix] [2023/05/13 18:21:51.183 +08:00] [DEBUG] [main.go:18] ["1024 HelloWorld true"]
[LogPrefix] [2023/05/13 18:21:51.183 +08:00] [INFO] [main.go:19] ["1024 HelloWorld true"]
[LogPrefix] [2023/05/13 18:21:51.183 +08:00] [NOTICE] [main.go:20] ["1024 HelloWorld true"]
[LogPrefix] [2023/05/13 18:21:51.183 +08:00] [WARN] [main.go:21] ["1024 HelloWorld true"]
[LogPrefix] [2023/05/13 18:21:51.183 +08:00] [ERROR] [main.go:22] ["1024 HelloWorld true"] [stack="main.main\n\t/Users/huangmeng/go/src/github.com/hslam/tmp/log/main.go:22\nruntime.main\n\t/usr/local/go/src/runtime/proc.go:250"]
[LogPrefix] [2023/05/13 18:21:51.183 +08:00] [PANIC] [main.go:27] ["1024 HelloWorld true"] [stack="main.main\n\t/Users/huangmeng/go/src/github.com/hslam/tmp/log/main.go:27\nruntime.main\n\t/usr/local/go/src/runtime/proc.go:250"]
[LogPrefix] [2023/05/13 18:21:51.183 +08:00] [FATAL] [main.go:25] ["1024 HelloWorld true"] [stack="main.main.func1\n\t/Users/huangmeng/go/src/github.com/hslam/tmp/log/main.go:25\nruntime.gopanic\n\t/usr/local/go/src/runtime/panic.go:884\nmain.main\n\t/Users/huangmeng/go/src/github.com/hslam/tmp/log/main.go:27\nruntime.main\n\t/usr/local/go/src/runtime/proc.go:250"]
Highlight
example
License

This package is licensed under a MIT license (Copyright (c) 2019 Meng Huang)

Author

log was written by Meng Huang.

Documentation

Overview

Package log implements multilevel logging.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func All

func All(v ...interface{})

All is equivalent to log.Print() for all log.

func Allf

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

Allf is equivalent to log.Printf() for all log.

func Allln

func Allln(v ...interface{})

Allln is equivalent to log.Println() for all log.

func Assert added in v1.0.2

func Assert(b bool)

Assert asserts that b is true. Otherwise, it would log fatal.

func Assertf added in v1.0.2

func Assertf(b bool, format string, v ...interface{})

Assertf is equivalent to Assert with info.

func Debug

func Debug(v ...interface{})

Debug is equivalent to log.Print() for debug.

func Debugf

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

Debugf is equivalent to log.Printf() for debug.

func Debugln

func Debugln(v ...interface{})

Debugln is equivalent to log.Println() for debug.

func Error

func Error(v ...interface{})

Error is equivalent to log.Print() for error.

func Errorf

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

Errorf is equivalent to log.Printf() for error.

func Errorln

func Errorln(v ...interface{})

Errorln is equivalent to log.Println() for error.

func Fatal

func Fatal(v ...interface{})

Fatal is equivalent to log.Print() for fatal.

func Fatalf

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

Fatalf is equivalent to log.Printf() for fatal.

func Fatalln

func Fatalln(v ...interface{})

Fatalln is equivalent to log.Println() for fatal.

func Flush added in v1.0.5

func Flush()

Flush writes any buffered data to the underlying io.Writer.

func GetPrefix

func GetPrefix() (prefix string)

GetPrefix returns log's prefix

func Info

func Info(v ...interface{})

Info is equivalent to log.Print() for info.

func Infof

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

Infof is equivalent to log.Printf() for info.

func Infoln

func Infoln(v ...interface{})

Infoln is equivalent to log.Println() for info.

func Notice

func Notice(v ...interface{})

Notice is equivalent to log.Print() for notice.

func Noticef

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

Noticef is equivalent to log.Printf() for notice.

func Noticeln

func Noticeln(v ...interface{})

Noticeln is equivalent to log.Println() for notice.

func Panic

func Panic(v ...interface{})

Panic is equivalent to log.Print() for panic.

func Panicf

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

Panicf is equivalent to log.Printf() for panic.

func Panicln

func Panicln(v ...interface{})

Panicln is equivalent to log.Println() for panic.

func SetBufferedOutput added in v1.0.5

func SetBufferedOutput(bufferSize int)

SetBufferedOutput sets the buffered writer with the buffer size.

func SetHighlight added in v1.0.2

func SetHighlight(highlight bool)

SetHighlight sets whether to enable the highlight field.

func SetLevel

func SetLevel(level Level)

SetLevel sets log's level

func SetLine added in v1.0.2

func SetLine(line bool)

SetLine sets whether to enable the line field .

func SetOut

func SetOut(w io.Writer)

SetOut sets log's writer. The out variable sets the destination to which log data will be written.

func SetPrefix

func SetPrefix(prefix string)

SetPrefix sets log's prefix

func SetShortLevel added in v1.0.2

func SetShortLevel(shortLevel bool)

SetShortLevel sets whether to enable the short level name.

func Trace

func Trace(v ...interface{})

Trace is equivalent to log.Print() for trace.

func Tracef

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

Tracef is equivalent to log.Printf() for trace.

func Traceln

func Traceln(v ...interface{})

Traceln is equivalent to log.Println() for trace.

func Warn

func Warn(v ...interface{})

Warn is equivalent to log.Print() for warn.

func Warnf

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

Warnf is equivalent to log.Printf() for warn.

func Warnln

func Warnln(v ...interface{})

Warnln is equivalent to log.Println() for warn.

Types

type Level

type Level uint8

Level defines the level for log. Higher levels log less info.

const (
	//AllLevel defines the lowest level.
	AllLevel Level = 0
	//TraceLevel defines the level of trace in test environments.
	TraceLevel Level = 1
	//DebugLevel defines the level of debug.
	DebugLevel Level = 2
	//InfoLevel defines the level of info.
	InfoLevel Level = 3
	//NoticeLevel defines the level of notice.
	NoticeLevel Level = 4
	//WarnLevel defines the level of warn.
	WarnLevel Level = 5
	//ErrorLevel defines the level of error.
	ErrorLevel Level = 6
	//PanicLevel defines the level of panic.
	PanicLevel Level = 7
	//FatalLevel defines the level of fatal.
	FatalLevel Level = 8
	//OffLevel defines the level of no log.
	OffLevel Level = 9
)

func GetLevel

func GetLevel() Level

GetLevel returns log's level

type Logger

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

Logger defines the logger.

func New

func New() *Logger

New creates a new Logger.

func (*Logger) All

func (l *Logger) All(v ...interface{})

All is equivalent to log.Print() for all log.

func (*Logger) Allf

func (l *Logger) Allf(format string, v ...interface{})

Allf is equivalent to log.Printf() for all log.

func (*Logger) Allln

func (l *Logger) Allln(v ...interface{})

Allln is equivalent to log.Println() for all log.

func (*Logger) Assert added in v1.0.2

func (l *Logger) Assert(b bool)

Assert asserts that b is true. Otherwise, it would log fatal.

func (*Logger) Assertf added in v1.0.2

func (l *Logger) Assertf(b bool, format string, v ...interface{})

Assertf is equivalent to Assert with info.

func (*Logger) Debug

func (l *Logger) Debug(v ...interface{})

Debug is equivalent to log.Print() for debug.

func (*Logger) Debugf

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

Debugf is equivalent to log.Printf() for debug.

func (*Logger) Debugln

func (l *Logger) Debugln(v ...interface{})

Debugln is equivalent to log.Println() for debug.

func (*Logger) Error

func (l *Logger) Error(v ...interface{})

Error is equivalent to log.Print() for error.

func (*Logger) Errorf

func (l *Logger) Errorf(format string, v ...interface{})

Errorf is equivalent to log.Printf() for error.

func (*Logger) Errorln

func (l *Logger) Errorln(v ...interface{})

Errorln is equivalent to log.Println() for error.

func (*Logger) Fatal

func (l *Logger) Fatal(v ...interface{})

Fatal is equivalent to log.Print() for fatal.

func (*Logger) Fatalf

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

Fatalf is equivalent to log.Printf() for fatal.

func (*Logger) Fatalln

func (l *Logger) Fatalln(v ...interface{})

Fatalln is equivalent to log.Println() for fatal.

func (*Logger) Flush added in v1.0.5

func (l *Logger) Flush()

Flush writes any buffered data to the underlying io.Writer.

func (*Logger) GetLevel

func (l *Logger) GetLevel() Level

GetLevel returns log's level

func (*Logger) GetPrefix

func (l *Logger) GetPrefix() (prefix string)

GetPrefix returns log's prefix

func (*Logger) Info

func (l *Logger) Info(v ...interface{})

Info is equivalent to log.Print() for info.

func (*Logger) Infof

func (l *Logger) Infof(format string, v ...interface{})

Infof is equivalent to log.Printf() for info.

func (*Logger) Infoln

func (l *Logger) Infoln(v ...interface{})

Infoln is equivalent to log.Println() for info.

func (*Logger) Notice

func (l *Logger) Notice(v ...interface{})

Notice is equivalent to log.Print() for notice.

func (*Logger) Noticef

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

Noticef is equivalent to log.Printf() for notice.

func (*Logger) Noticeln

func (l *Logger) Noticeln(v ...interface{})

Noticeln is equivalent to log.Println() for notice.

func (*Logger) Panic

func (l *Logger) Panic(v ...interface{})

Panic is equivalent to log.Print() for panic.

func (*Logger) Panicf

func (l *Logger) Panicf(format string, v ...interface{})

Panicf is equivalent to log.Printf() for panic.

func (*Logger) Panicln

func (l *Logger) Panicln(v ...interface{})

Panicln is equivalent to log.Println() for panic.

func (*Logger) SetBufferedOutput added in v1.0.5

func (l *Logger) SetBufferedOutput(bufferSize int)

SetBufferedOutput sets the buffered writer with the buffer size.

func (*Logger) SetHighlight added in v1.0.2

func (l *Logger) SetHighlight(highlight bool)

SetHighlight sets whether to enable the highlight field.

func (*Logger) SetLevel

func (l *Logger) SetLevel(level Level)

SetLevel sets log's level

func (*Logger) SetLine added in v1.0.2

func (l *Logger) SetLine(line bool)

SetLine sets whether to enable the line field .

func (*Logger) SetOut

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

SetOut sets log's writer. The out variable sets the destination to which log data will be written.

func (*Logger) SetPrefix

func (l *Logger) SetPrefix(prefix string)

SetPrefix sets log's prefix

func (*Logger) SetShortLevel added in v1.0.2

func (l *Logger) SetShortLevel(shortLevel bool)

SetShortLevel sets whether to enable the short level name.

func (*Logger) Trace

func (l *Logger) Trace(v ...interface{})

Trace is equivalent to log.Print() for trace.

func (*Logger) Tracef

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

Tracef is equivalent to log.Printf() for trace.

func (*Logger) Traceln

func (l *Logger) Traceln(v ...interface{})

Traceln is equivalent to log.Println() for trace.

func (*Logger) Warn

func (l *Logger) Warn(v ...interface{})

Warn is equivalent to log.Print() for warn.

func (*Logger) Warnf

func (l *Logger) Warnf(format string, v ...interface{})

Warnf is equivalent to log.Printf() for warn.

func (*Logger) Warnln

func (l *Logger) Warnln(v ...interface{})

Warnln is equivalent to log.Println() for warn.

Jump to

Keyboard shortcuts

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