I want to generate four log files for warning, debug, error, and info. I want to control which file should be print at run time

huangapple go评论69阅读模式
英文:

I want to generate four log files for warning, debug, error, and info. I want to control which file should be print at run time

问题

包Loggers

导入 (
"io"
"os"
"log"
)

var logCloser io.Closer

func MyLogger() {
logFile, err := os.OpenFile("C:/Git/goclassec/src/gclassec/Logs/log.txt", os.O_CREATE|os.O_APPEND|os.O_RDWR, 0666)
if err != nil {
panic(err)
}
logCloser = logFile
log.SetOutput(logFile)
}

func CloseMyLogger() {
logCloser.Close()
}

英文:
package Loggers

import (
	"io"
	"os"
	"log"
)

var logCloser io.Closer
func MyLogger() {
	logFile, err := os.OpenFile("C:/Git/goclassec/src/gclassec/Logs/log.txt", os.O_CREATE|os.O_APPEND|os.O_RDWR, 0666)
	if err != nil {
		panic(err)
	}
	logCloser = logFile
	log.SetOutput(logFile)
}
func CloseMyLogger() {
	logCloser.Close()
}

答案1

得分: 1

你可以使用log.New来创建不同的日志记录器,并将其包装在你的MyLogger中。

这是New的格式:

New(out io.Writer, prefix string, flag int)

这是一个示例实现:

代码

package loggers

import (
	"log"
	"os"
)

// MyLogger 自定义日志记录器
type MyLogger struct {
	ErrorL *log.Logger
	InfoL  *log.Logger
	DebugL *log.Logger
}

// New 返回日志记录器
// TODO: 处理错误
func New() *MyLogger {
	ml := new(MyLogger)
	if logFile, err := os.OpenFile("C:/Git/goclassec/src/gclassec/Logs/log_error.txt", os.O_CREATE|os.O_APPEND|os.O_RDWR, 0666); err == nil {
		ml.ErrorL = log.New(logFile, "ERROR", log.LUTC)
	}
	if logFile, err := os.OpenFile("C:/Git/goclassec/src/gclassec/Logs/log_info.txt", os.O_CREATE|os.O_APPEND|os.O_RDWR, 0666); err == nil {
		ml.InfoL = log.New(logFile, "INFO", log.LUTC)
	}
	if logFile, err := os.OpenFile("C:/Git/goclassec/src/gclassec/Logs/log_debug.txt", os.O_CREATE|os.O_APPEND|os.O_RDWR, 0666); err == nil {
		ml.DebugL = log.New(logFile, "DEBUG", log.LUTC)
	}
	return ml
}

func (ml MyLogger) Error(data ...interface{}) {
	ml.ErrorL.Println(data...)
}

func (ml MyLogger) Info(data ...interface{}) {
	ml.InfoL.Println(data...)
}

func (ml MyLogger) Debug(data ...interface{}) {
	ml.DebugL.Println(data...)
}

用法

logger := loggers.New()
logger.Error("Error occurred", err)
logger.Info("Doing", "something", "again", "blah")
英文:

You can use log.New to create different loggers and wrap it inside your MyLogger

This is the New format

 New(out io.Writer, prefix string, flag int)

Here is a sample implementation

Code

package loggers
import (
"log"
"os"
)
//MyLogger custom logger
type MyLogger struct {
ErrorL *log.Logger
InfoL  *log.Logger
DebugL *log.Logger
}
//New returns logger
//TODO : deal with error
func New() *MyLogger {
ml := new(MyLogger)
if logFile, err := os.OpenFile("C:/Git/goclassec/src/gclassec/Logs/log_error.txt", os.O_CREATE|os.O_APPEND|os.O_RDWR, 0666); err == nil {
ml.ErrorL = log.New(logFile, "ERROR", log.LUTC)
}
if logFile, err := os.OpenFile("C:/Git/goclassec/src/gclassec/Logs/log_info.txt", os.O_CREATE|os.O_APPEND|os.O_RDWR, 0666); err == nil {
ml.InfoL = log.New(logFile, "INFO", log.LUTC)
}
if logFile, err := os.OpenFile("C:/Git/goclassec/src/gclassec/Logs/log_debug.txt", os.O_CREATE|os.O_APPEND|os.O_RDWR, 0666); err == nil {
ml.DebugL = log.New(logFile, "DEBUG", log.LUTC)
}
return ml
}
func (ml MyLogger) Error(data ...interface{}) {
ml.ErrorL.Println(data...)
}
func (ml MyLogger) Info(data ...interface{}) {
ml.InfoL.Println(data...)
}
func (ml MyLogger) Debug(data ...interface{}) {
ml.DebugL.Println(data...)
}

Usage

logger := loggers.New()
logger.Error("Error occured",err)
logger.Info("Doing","something","again","blah")

huangapple
  • 本文由 发表于 2017年2月10日 15:47:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/42154076.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定