Skip to content
/ log Public

Package log implements multilevel logging.

License

Notifications You must be signed in to change notification settings

hslam/log

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

47 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.