log

package
v0.0.0-...-51dca1d Latest Latest
Warning

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

Go to latest
Published: Dec 13, 2024 License: GPL-3.0 Imports: 14 Imported by: 167

README

接口简介

  • NewLogger

    创建 Logger 对象, 需要传递一个日志名称, 如 "daemon/network"

  • Logger.SetLogLevel

    设置日志记录级别, 默认为 Logger.LevelInfo

  • Logger.Debug, Logger.Info, Logger.Warning, Logger.Error

    日志记录接口, 其中 Logger.Error() 会额外打印 go 函数调用轨迹 (trace)

  • Logger.Debugf, Logger.Infof, Logger.Warningf, Logger.Errorf

    日志记录接口, 支持 format 格式, 语法风格和 fmt.Printf() 相同

  • Logger.Panic

    记录日志并额外执行一条 panic() 语句

  • Logger.Fatal

    记录日志并额外执行 os.Exit(1)

环境变量

  • DDE_DEBUG, 若值不为空, 则打印所有日志, 启用其他 DDE_DEBUG_XXX 变 量时, 该变量会默认启用
  • DDE_DEBUG_MATCH, 仅允许匹配该环境变量的 logger 对象打印i日志, 对 logger name 进行匹配, 不区分大小写
  • DDE_DEBUG_LEVEL, 用于从外部设置日志打印级别, 可选值为 "debug", "info", "warning", "error", "fatal"
  • DDE_DEBUG_CONSOLE, 若值不为空, 则以 syslog 格式打印终端日志

示例 1, 打印 startdde 所有日志:

env DDE_DEBUG=1 /usr/bin/startdde

示例 2, 仅打印 startdde 警告级别以上的日志:

env DDE_DEBUG_LEVEL="warning" startddek

示例 3, 打印 dde-daemon 中网络模块的所有日志:

env DDE_DEBUG_MATCH="network" dde-session-daemon

示例 4, 打印 dde-daemon 中网络模块的日志, 且仅打印警告级别以上的日志:

env DDE_DEBUG_MATCH="network" DDE_DEBUG_LEVEL="warning" dde-session-daemon

示例代码

import "github.com/linuxdeepin/go-lib/log"
import "flag"

var (
  l = log.NewLogger("daemon/test")
  argDebug bool
)

func main() {
  defer func() {
      if err := recover(); err != nil {
          l.Fatal(err)
      }
  }()

  // parse arguments
  flag.BoolVar(&argDebug, "d", false, "debug")
  flag.Parse()

  // setup logger
  if argDebug {
      l.SetLogLevel(l.LevelDebug)
  }
}

查看日志

对于 rsyslog, 可以配合辅助工具 logtool 高亮显示日志条目, 更加方便

# apt-get install logtool
$ tailf /var/log/syslog | logtool # follow syslog
$ tailf /var/log/syslog | grep "startdde"  | logtool # filter startdde syslog items

对于 systemd,则可以使用 journalctl

$ journalctl -f # follow syslog
$ journalctl /usr/bin/startdde # filter startdde syslog items

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// DebugEnv is the name of environment variable that used to
	// enable debug mode , if exists the default log level will be
	// "LevelDebug".
	DebugEnv = defaultDebugEnv

	// DebugLevelEnv is the name of environment variable that used to
	// control the log level, could be "debug", "info", "warning",
	// "error", "fatal" and "disable".
	DebugLevelEnv = defaultDebugLelveEnv

	// DebugMatchEnv is the name of environment variable that used to
	// enable debug mode for target logger object.
	DebugMatchEnv = defaultDebugMatchEnv

	// DebugFile if the file name that if exist the default log level
	// will be "LevelDebug".
	DebugFile = defaultDebugFile
)
View Source
var (
	// DebugConsoleEnv is the name of environment variable that used to control
	// the console backend print log in syslog format.
	DebugConsoleEnv = defaultDebugConsoleEnv
)
View Source
var (
	// SyslogTagPrefix define the prefix of syslog tag, default is
	// empty.
	SyslogTagPrefix = defaultSyslogTagPrefix
)

Functions

This section is empty.

Types

type Backend

type Backend interface {
	// contains filtered or unexported methods
}

Backend defines interface of logger's back-ends.

type Logger

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

Logger is a wrapper object to access Logger dbus service.

func NewLogger

func NewLogger(name string) (l *Logger)

NewLogger create a Logger object, which need a string as name to register Logger dbus service, if the environment variable exists which name stores in variable "DebugEnv", the default log level will be "LevelDebug" or is "LevelInfo".

func (*Logger) AddBackend

func (l *Logger) AddBackend(b Backend) bool

AddBackend append a log back-end.

func (*Logger) AddBackendConsole

func (l *Logger) AddBackendConsole() bool

AddBackendConsole append a console back-end.

func (*Logger) AddBackendSyslog

func (l *Logger) AddBackendSyslog() bool

AddBackendSyslog append a syslog back-end.

func (*Logger) AddExtArgForRestart

func (l *Logger) AddExtArgForRestart(arg string)

AddExtArgForRestart add the command option which be used when process fataled and restart by Logger dbus service.

func (*Logger) BeginTracing

func (l *Logger) BeginTracing()

BeginTracing log function information when entering it.

func (*Logger) Debug

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

Debug log a message in "debug" level.

func (*Logger) Debugf

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

Debugf formats message according to a format specifier and log it in "debug" level.

func (*Logger) EndTracing

func (l *Logger) EndTracing()

EndTracing log function information when leaving it.

func (*Logger) Error

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

Error log a message in "error" level.

func (*Logger) Errorf

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

Errorf formats message according to a format specifier and log it in "error" level.

func (*Logger) Fatal

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

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

func (*Logger) Fatalf

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

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

func (*Logger) GetLogLevel

func (l *Logger) GetLogLevel() Priority

GetLogLevel return the log level.

func (*Logger) Info

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

Info log a message in "info" level.

func (*Logger) Infof

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

Infof formats message according to a format specifier and log it in "info" level.

func (*Logger) Panic

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

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

func (*Logger) Panicf

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

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

func (*Logger) RemoveBackend

func (l *Logger) RemoveBackend(b Backend)

RemoveBackend remove all back-end with target type.

func (*Logger) RemoveBackendConsole

func (l *Logger) RemoveBackendConsole()

RemoveBackendConsole remove all console back-end.

func (*Logger) RemoveBackendSyslog

func (l *Logger) RemoveBackendSyslog()

RemoveBackendSyslog remove all console back-end.

func (*Logger) ResetBackends

func (l *Logger) ResetBackends()

ResetBackends clear all backends.

func (*Logger) SetLogLevel

func (l *Logger) SetLogLevel(level Priority) *Logger

SetLogLevel reset the log level.

func (*Logger) SetRestartCommand

func (l *Logger) SetRestartCommand(exefile string, args ...string)

SetRestartCommand reset the command and argument when restart after fatal.

func (*Logger) Warning

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

Warning log a message in "warning" level.

func (*Logger) Warningf

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

Warningf formats message according to a format specifier and log it in "warning" level.

type Priority

type Priority int

Priority is the data type of log level.

const (
	LevelDisable Priority = iota
	LevelFatal
	LevelPanic
	LevelError
	LevelWarning
	LevelInfo
	LevelDebug
)

Definitions of log level, the larger of the value, the higher of the priority.

Jump to

Keyboard shortcuts

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