log

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2024 License: MIT Imports: 5 Imported by: 22

README

Using gwaylib/log

The number of logs should not be too many in the release program, which may affect the program performance.
In the call, the logger adopts the function of asynchronous cache to improve the execution efficiency of the log. The cache value is determined by the adapter implements, and the default is 10 items cache.
The log should be closed after use to ensure that the cached data can be output. If the cached data cannot be output, the error will be transferred by the adapter.

About using log level

DEBUG
Debugging information, output only during debugging.
It is controlled by the platform and can be output to the console, which is equivalent to fmt.Print output.

INFO
Program running status change information.
Such as start, stop, reconnection and other information, which reflects the change state of the program environment.

WARN
Program exception information.
This category does not affect the continued use of the program, but its results may lead to potential major problems.
For example: some unknown errors; Network connection error (but it can be repaired automatically after reconnection), connection timeout, etc.
If there are too many such exceptions in a period of time, the causes should be analyzed, such as possible problems:
Being attacked, hardware aging, hardware reaching the upper limit of bearing capacity, abnormal service of the other party, etc.
The log adapter recommends sending an email to relevant personnel.

ERROR
Program fatal error message.
This error will affect the normal logic, and even the platform panic and stop the service.
For example, behaviors that need to be handled in time, such as database unavailable, recharge unavailable, SMS unavailable, and Vos unavailable, can be defined as this category.
The log adapter recommends sending an email, real-time SMS or telephone (or other real-time contact information) to notify relevant personnel.

FATAL
The program ended abnormally.
The log adapter recommends calling all real-time contact information and contacting relevant personnel for processing.

Default use case:

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

func main() {
  log.Info("OK")
}

Custom a logger

import (
  "github.com/gwaylib/log"
  "github.com/gwaylib/logger"
)

var lg *logger.Log

func init() {
  // make a custom logger 
  level, _ := strconv.Atoi(os.Getenv(log.GWAYLIB_LOG_LEVEL))
  adapter := []logger.Adapter{stdio.New(os.Stdout)}
  callerDepth := 4 // if callerDepth == 0, close the file path caller

  // replace the default logger
  log.Log = logger.New(&logger.DefaultContext, "appname", callerDepth, proto.Level(level), adapter...)

  // or
  // make a new package log
  lg = logger.New(&logger.DefaultContext, "appname", callerDepth, proto.Level(level), adapter...)

  // or
  // make a new package default log
  //lg = logger.NewDefaultLogger(loggerName, adapter...)

  // or 
  // copy the log.go file in local and fix it
}

func main() {
  log.Info("OK")
  lg.Info("OK")
}

Documentation

Index

Constants

View Source
const (
	// Setting the log level
	// 0 debug or above
	// 1 info or above
	// 2 warn or above
	// 3 error or above
	// 4 fatal
	GWAYLIB_LOG_LEVEL = "GWAYLIB_LOG_LEVEL"
)

Variables

View Source
var (
	Log = newWithCaller("", 4)
)

Functions

func Debug

func Debug(msg ...interface{})

Debug level, for developer println the debug message.

值 0 调试信息,不提交服务器. 由平台控制,可控制台输出,相当于fmt.Print输出.

func Debugf

func Debugf(f string, msg ...interface{})

func Error

func Error(msg ...interface{})

Error level, for status is error, need someone process the erorr, system refuse the service, but it still can be run. The Log system should call the mainter immediately.

值 3 程序致命的错误信息, 提交服务器。此错误将影响到正常逻辑, 甚至平台因此而恐慌、停止服务的行为. 例如:数据库不可用、充值不可用、短信不可用、vos不可用等需要及时处理的行为都可定义为此类别。 日志系统将发送一封邮件、短信(或者其他实时联系方式)至相关人员

func Errorf

func Errorf(f string, msg ...interface{})

func Exit

func Exit(code int, msg ...interface{})

Exit the program with message log.

退出操作,执行关闭日志操作并调用os.Exit 对于可执行程序来说,日志的退出意味着程序的退出。

Params

code -- code to call os.Exit
msg -- message log in information level.

func Fatal

func Fatal(msg ...interface{})

Fatal level, the status is fatal, if continue run the program, it will cause irreparable things The Log system should call the mainter immediately.

值 4 检测到程序非正常结束。 日志系统应调用所有实时联系方式联系相关人员处理。

func Fatalf

func Fatalf(f string, msg ...interface{})

func Info

func Info(msg ...interface{})

Information level, for status changed Example: boot, stop, reconnect, which changed the program enviroment. 值 1 程序运行状态信息,提交服务器. 如启动、停止、重连等信息,体现了程序环境的变更状态

func Infof

func Infof(f string, msg ...interface{})

func New

func New(loggerName string) proto.Logger

func Panic

func Panic(msg ...interface{})

Same as Fatal

func Panicf

func Panicf(f string, msg ...interface{})

func Print

func Print(v ...interface{})

Same as Info, golang log interface.

func Printf

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

Same as Infof, golang log interface.

func Println

func Println(v ...interface{})

Same as Info, golang log interface.

func Warn

func Warn(msg ...interface{})

Warning level, for status warning, maybe the status should be broken soon, but also maybe auto recover soon. Expect send a warning mail to maintaner.

值 2 程序异常信息,提交服务器. 本类别不影响程序继续使用,但其结果可能会引出潜在的重大问题. 例如:请求的数据格式错误;网络连接错误(但重新连接后可自动修复), 连接超时等行为。 此类异常在一段时间如果出现过多,那么应该分析其中的原因,例如可能存在的问题: 被攻击、硬件老化、硬件达到了承载上限、对方服务出现异常等问题。 日志系统将发送一封邮件到相关人员。

func Warnf

func Warnf(f string, msg ...interface{})

Types

This section is empty.

Directories

Path Synopsis
adapter
Deprecated, use redismsq instead
Deprecated, use redismsq instead

Jump to

Keyboard shortcuts

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