golang log的使用和处理

Golang · Fecmall · 于 5年前 发布 · 3592 次阅读

使用github库包:github.com/sirupsen/logrus

package main

import (
	"os"
	"xxxxx/logging"
	"github.com/sirupsen/logrus"
)

var log = logging.MustGetLogger()
func main() {
	setupLogging()
	log.Fatal(err)
	log.Fatal("error reading map_data.json: ")
}	

func setupLogging() {
	logrus.SetLevel(logrus.DebugLevel)

	logPath := "./logs/log.log"
	file, err := os.OpenFile(logPath, os.O_CREATE|os.O_APPEND|os.O_RDWR, 0777)
	if err != nil {
		log.Fatal("Cannot log to file", err.Error())
	}

	logrus.SetFormatter(&logrus.JSONFormatter{})
	logrus.SetOutput(file)
}

xxxxx/logging 包 logging.go


package logging

import (
	"github.com/sirupsen/logrus"
	"os"
	"time"
	"runtime"
	"strings"
	"fmt"
)

type TixLogger struct {
	Hostname string
	*logrus.Logger
}

func MustGetLogger() *TixLogger {
	hostname, err := os.Hostname()
	if err != nil {
		hostname = "unknown"
	}

	tixLogger := &TixLogger{hostname, logrus.StandardLogger()}
	return tixLogger
}

func (tx *TixLogger) Info(args ...interface{}) {
	fields(tx).Info(args...)
}

func (tx *TixLogger) Infof(format string, args ...interface{}) {
	fields(tx).Infof(format, args...)
}

func (tx *TixLogger) Debug(args ...interface{}) {
	fields(tx).Debug(args...)
}

func (tx *TixLogger) Debugf(format string, args ...interface{}) {
	fields(tx).Debugf(format, args...)
}

func (tx *TixLogger) Warn(args ...interface{}) {
	fields(tx).Warn(args...)
}

func (tx *TixLogger) Warnf(format string, args ...interface{}) {
	fields(tx).Warnf(format, args...)
}

func (tx *TixLogger) Error(args ...interface{}) {
	fields(tx).Error(args...)
}

func (tx *TixLogger) Errorf(format string, args ...interface{}) {
	fields(tx).Errorf(format, args...)
}

// DebugfWithId write formatted debug level log with added log_id field
func (tx *TixLogger) DebugfWithId(id string, format string, args ...interface{}) {
	fields(tx).WithField("log_id", id).Debugf(format, args...)
}

// InfofWithId write formatted info level log with added log_id field
func (tx *TixLogger) InfofWithId(id string, format string, args ...interface{}) {
	fields(tx).WithField("log_id", id).Infof(format, args...)
}

// InfoWithId write info level log with added log_id field
func (tx *TixLogger) InfoWithId(id string, args ...interface{}) {
	fields(tx).WithField("log_id", id).Info(args...)
}

// ErrorfWithId write formatted error level log with added log_id field
func (tx *TixLogger) ErrorfWithId(id string, format string, args ...interface{}) {
	fields(tx).WithField("log_id", id).Errorf(format, args...)
}

// ErrorWithId write error level log with added log_id field
func (tx *TixLogger) ErrorWithId(id string, args ...interface{}) {
	fields(tx).WithField("log_id", id).Error(args...)
}

func fields(tx *TixLogger) *logrus.Entry {
	file, line := getCaller()
	return tx.Logger.WithField("hostname", tx.Hostname).WithField("time", time.Now().UTC().Format(time.RFC3339)).WithField("source", fmt.Sprintf("%s:%d", file, line))
}

func getCaller() (string, int) {
	_, file, line, ok := runtime.Caller(3)
	if !ok {
		file = "<???>"
		line = 1
	} else {
		slash := strings.LastIndex(file, "/")
		file = file[slash+1:]
	}
	return file, line
}


可以通过log.Fatal(err), 将信息写入log文件。

如果想要根据配置进行log的输出到文件,还是直接输出到屏幕,可以通过配置中添加

[switch]
cronjob = "on"
log = "off"

将main包里面的

func setupLogging() {
	logrus.SetLevel(logrus.DebugLevel)
	if config.MustGetString("switch.log") == "on" {
		log.Info("here")
		logPath := config.MustGetString("server.log_path")

		file, err := os.OpenFile(logPath, os.O_CREATE|os.O_APPEND|os.O_RDWR, 0777)
		if err != nil {
			log.Fatal("Cannot log to file", err.Error())
		}

		logrus.SetFormatter(&logrus.JSONFormatter{})
		logrus.SetOutput(file)
	}
}

通过配置文件来决定,log直接显示到屏幕,还是写入到log文件中。

共收到 0 条回复
没有找到数据。
添加回复 (需要登录)
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册
Your Site Analytics