日志方法增加异步与同步的方法
This commit is contained in:
@@ -14,13 +14,20 @@ func main() {
|
||||
log.Fatal("Failed to load config:", err)
|
||||
}
|
||||
|
||||
// 使用工厂创建日志记录器(推荐方式)
|
||||
// 方式1:使用工厂创建日志记录器(推荐方式)
|
||||
fac := factory.NewFactory(cfg)
|
||||
logger, err := fac.GetLogger()
|
||||
if err != nil {
|
||||
log.Fatal("Failed to create logger:", err)
|
||||
}
|
||||
|
||||
// 如果使用异步模式,程序退出前需要关闭logger
|
||||
defer logger.Close()
|
||||
|
||||
// 方式2:直接使用工厂的日志方法(黑盒模式,更简单)
|
||||
// fac.LogInfo("Application started")
|
||||
// fac.LogError("An error occurred")
|
||||
|
||||
// 示例1:基本日志记录
|
||||
logger.Info("Application started")
|
||||
logger.Debug("Debug message: %s", "This is a debug message")
|
||||
@@ -45,6 +52,11 @@ func main() {
|
||||
logger.Warn("This is a warn log")
|
||||
logger.Error("This is an error log")
|
||||
|
||||
// 示例4:异步模式使用
|
||||
// 如果配置中设置了 "async": true,日志会异步写入
|
||||
// 程序退出前需要调用 Close() 确保所有日志写入完成
|
||||
// logger.Close()
|
||||
|
||||
// 注意:Fatal和Panic会终止程序,示例中不执行
|
||||
// logger.Fatal("This would exit the program")
|
||||
// logger.Panic("This would panic")
|
||||
|
||||
Reference in New Issue
Block a user