beego框架-logs模块学习笔记

2019-02-25 11:57:52 The small youth whoopen in new window 阅读数 330更多

分类专栏: beegoopen in new window

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SAopen in new window 版权协议,转载请附上原文出处链接和本声明。

本文链接:https://blog.csdn.net/qq_30549833/article/details/87914515open in new window

前一段时间的项目中用到了beego框架下的logs模块,记录一下使用过程。logs模块官方文档open in new window

一、示例

1、控制台输出

//控制台输出 func Console() { log := logs.NewLogger() log.SetLogger(logs.AdapterConsole) //设置打印函数及行号 log.EnableFuncCallDepth(true) log.Debug("log1---> my book is bought in the year of ") log.Critical("log1---> oh,crash") }

2、文件输出

//文件输出 func ToFile() { log := logs.NewLogger() log.SetLogger(logs.AdapterFile, `{"filename":"log2.log","maxlines":1000,"maxsize":1000,"daily":true,"maxdays":10,"color":true}`) //l.SetLogger(logs.AdapterFile, `{"filename":"project.log","level":7,"maxlines":1000,"maxsize":1000,"daily":true,"maxdays":10,"color":true}`) log.EnableFuncCallDepth(true) log.SetLogFuncCallDepth(2) log.Debug("log2---> my book is bought in the year of ") log.Critical("log2---> oh,crash") }

文件格式说明:

3、控制台和文件同时输出

//控制台,文件同时输出 func consoleAndFile() { log := logs.NewLogger() log.SetLogger(logs.AdapterConsole) log.SetLogger(logs.AdapterFile, `{"filename":"log3-6.log","level":6,"maxlines":1000,"maxsize":1000,"daily":true,"maxdays":10,"color":true}`) log.EnableFuncCallDepth(true) log.SetLogFuncCallDepth(2) //log.Async(1e3) //1-7级别递减,默认是trace,显示当前数字以前的级别,例如:3时,显示【Emergency】【Alert】【Critical】【Error】 log.Emergency("log3--->Emergency") log.Alert("log3--->Alert") //1 log.Critical("log3--->Critical") //2 log.Error("log3--->Error") //3 log.Warn("log3--->Warning") //4 log.Notice("log3--->Notice") //5 log.Info("log3--->Info") //6 log.Debug("log3--->Debug") //7 log.Trace("log3--->Trace") }

日志级别说明:

例如:3时,显示【Emergency】【Alert】【Critical】【Error】

4、网络传输

//网络数据 func conn() { log := logs.NewLogger() //udp 传输到本地端口7020 //将日志通过tcp的传输方式发送到ip为172.20.36.141,端口为:7020的主机上 log.SetLogger(logs.AdapterConn, `{"net":"tcp","addr":"172.20.36.141:7020"}`) log.EnableFuncCallDepth(true) log.SetLogFuncCallDepth(2) log.Emergency("log4--->Emergency") }

tcp时logs端作为客户端连接远端服务

udp时直传数据:

5、邮件发送

//邮件发送 func smtp() { log := logs.NewLogger() log.SetLogger(logs.AdapterMail, `{"username":"luciferofwg@gmail.com","password":"xxxxxxxxx","host":"imap.gmail.com:993","sendTos":["958730879@qq.com"]}`) log.EnableFuncCallDepth(true) log.SetLogFuncCallDepth(2) log.Emergency("log5--->Emergency") }

二、小结

1、函数行号打印深度

默认是2,对logs模块封装后可以通过修改该值来改变函数及行号的深度

log.SetLogFuncCallDepth(`深度`)

三、完成代码

完成代码open in new window

Last Updated:
Contributors: 刘荣杰