英文:
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")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论