Format and remove 12.1.md spaces
This commit is contained in:
250
zh/12.1.md
250
zh/12.1.md
@@ -7,7 +7,7 @@ logrus是用Go语言实现的一个日志系统,与标准库log完全兼容并
|
||||
首先安装logrus
|
||||
```Go
|
||||
|
||||
go get -u github.com/sirupsen/logrus
|
||||
go get -u github.com/sirupsen/logrus
|
||||
|
||||
```
|
||||
|
||||
@@ -15,65 +15,65 @@ logrus是用Go语言实现的一个日志系统,与标准库log完全兼容并
|
||||
|
||||
```Go
|
||||
|
||||
package main
|
||||
package main
|
||||
|
||||
import (
|
||||
log "github.com/Sirupsen/logrus"
|
||||
)
|
||||
import (
|
||||
log "github.com/Sirupsen/logrus"
|
||||
)
|
||||
|
||||
func main() {
|
||||
log.WithFields(log.Fields{
|
||||
"animal": "walrus",
|
||||
}).Info("A walrus appears")
|
||||
}
|
||||
func main() {
|
||||
log.WithFields(log.Fields{
|
||||
"animal": "walrus",
|
||||
}).Info("A walrus appears")
|
||||
}
|
||||
```
|
||||
### 基于logrus的自定义日志处理
|
||||
```Go
|
||||
|
||||
package main
|
||||
package main
|
||||
|
||||
import (
|
||||
log "github.com/Sirupsen/logrus"
|
||||
"os"
|
||||
)
|
||||
import (
|
||||
log "github.com/Sirupsen/logrus"
|
||||
"os"
|
||||
)
|
||||
|
||||
func init() {
|
||||
// 日志格式化为JSON而不是默认的ASCII
|
||||
log.SetFormatter(&log.JSONFormatter{})
|
||||
func init() {
|
||||
// 日志格式化为JSON而不是默认的ASCII
|
||||
log.SetFormatter(&log.JSONFormatter{})
|
||||
|
||||
// 输出stdout而不是默认的stderr,也可以是一个文件
|
||||
log.SetOutput(os.Stdout)
|
||||
// 输出stdout而不是默认的stderr,也可以是一个文件
|
||||
log.SetOutput(os.Stdout)
|
||||
|
||||
// 只记录严重或以上警告
|
||||
log.SetLevel(log.WarnLevel)
|
||||
}
|
||||
// 只记录严重或以上警告
|
||||
log.SetLevel(log.WarnLevel)
|
||||
}
|
||||
|
||||
func main() {
|
||||
log.WithFields(log.Fields{
|
||||
"animal": "walrus",
|
||||
"size": 10,
|
||||
}).Info("A group of walrus emerges from the ocean")
|
||||
func main() {
|
||||
log.WithFields(log.Fields{
|
||||
"animal": "walrus",
|
||||
"size": 10,
|
||||
}).Info("A group of walrus emerges from the ocean")
|
||||
|
||||
log.WithFields(log.Fields{
|
||||
"omg": true,
|
||||
"number": 122,
|
||||
}).Warn("The group's number increased tremendously!")
|
||||
log.WithFields(log.Fields{
|
||||
"omg": true,
|
||||
"number": 122,
|
||||
}).Warn("The group's number increased tremendously!")
|
||||
|
||||
log.WithFields(log.Fields{
|
||||
"omg": true,
|
||||
"number": 100,
|
||||
}).Fatal("The ice breaks!")
|
||||
log.WithFields(log.Fields{
|
||||
"omg": true,
|
||||
"number": 100,
|
||||
}).Fatal("The ice breaks!")
|
||||
|
||||
// 通过日志语句重用字段
|
||||
// logrus.Entry返回自WithFields()
|
||||
contextLogger := log.WithFields(log.Fields{
|
||||
"common": "this is a common field",
|
||||
"other": "I also should be logged always",
|
||||
})
|
||||
// 通过日志语句重用字段
|
||||
// logrus.Entry返回自WithFields()
|
||||
contextLogger := log.WithFields(log.Fields{
|
||||
"common": "this is a common field",
|
||||
"other": "I also should be logged always",
|
||||
})
|
||||
|
||||
contextLogger.Info("I'll be logged with common and other field")
|
||||
contextLogger.Info("Me too")
|
||||
}
|
||||
contextLogger.Info("I'll be logged with common and other field")
|
||||
contextLogger.Info("Me too")
|
||||
}
|
||||
```
|
||||
|
||||
## seelog介绍
|
||||
@@ -95,20 +95,20 @@ seelog是用Go语言实现的一个日志系统,它提供了一些简单的函
|
||||
首先安装seelog
|
||||
```Go
|
||||
|
||||
go get -u github.com/cihub/seelog
|
||||
go get -u github.com/cihub/seelog
|
||||
|
||||
```
|
||||
然后我们来看一个简单的例子:
|
||||
```Go
|
||||
|
||||
package main
|
||||
package main
|
||||
|
||||
import log "github.com/cihub/seelog"
|
||||
import log "github.com/cihub/seelog"
|
||||
|
||||
func main() {
|
||||
defer log.Flush()
|
||||
log.Info("Hello from Seelog!")
|
||||
}
|
||||
func main() {
|
||||
defer log.Flush()
|
||||
log.Info("Hello from Seelog!")
|
||||
}
|
||||
|
||||
```
|
||||
编译后运行如果出现了`Hello from seelog`,说明seelog日志系统已经成功安装并且可以正常运行了。
|
||||
@@ -117,59 +117,59 @@ seelog是用Go语言实现的一个日志系统,它提供了一些简单的函
|
||||
seelog支持自定义日志处理,下面是我基于它自定义的日志处理包的部分内容:
|
||||
```Go
|
||||
|
||||
package logs
|
||||
|
||||
import (
|
||||
// "errors"
|
||||
"fmt"
|
||||
seelog "github.com/cihub/seelog"
|
||||
// "io"
|
||||
)
|
||||
|
||||
var Logger seelog.LoggerInterface
|
||||
|
||||
func loadAppConfig() {
|
||||
appConfig := `
|
||||
<seelog minlevel="warn">
|
||||
<outputs formatid="common">
|
||||
<rollingfile type="size" filename="/data/logs/roll.log" maxsize="100000" maxrolls="5"/>
|
||||
<filter levels="critical">
|
||||
<file path="/data/logs/critical.log" formatid="critical"/>
|
||||
<smtp formatid="criticalemail" senderaddress="astaxie@gmail.com" sendername="ShortUrl API" hostname="smtp.gmail.com" hostport="587" username="mailusername" password="mailpassword">
|
||||
<recipient address="xiemengjun@gmail.com"/>
|
||||
</smtp>
|
||||
</filter>
|
||||
</outputs>
|
||||
<formats>
|
||||
<format id="common" format="%Date/%Time [%LEV] %Msg%n" />
|
||||
<format id="critical" format="%File %FullPath %Func %Msg%n" />
|
||||
<format id="criticalemail" format="Critical error on our server!\n %Time %Date %RelFile %Func %Msg \nSent by Seelog"/>
|
||||
</formats>
|
||||
</seelog>
|
||||
`
|
||||
logger, err := seelog.LoggerFromConfigAsBytes([]byte(appConfig))
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
UseLogger(logger)
|
||||
}
|
||||
|
||||
func init() {
|
||||
DisableLog()
|
||||
loadAppConfig()
|
||||
}
|
||||
|
||||
// DisableLog disables all library log output
|
||||
func DisableLog() {
|
||||
Logger = seelog.Disabled
|
||||
}
|
||||
|
||||
// UseLogger uses a specified seelog.LoggerInterface to output library log.
|
||||
// Use this func if you are using Seelog logging system in your app.
|
||||
func UseLogger(newLogger seelog.LoggerInterface) {
|
||||
Logger = newLogger
|
||||
package logs
|
||||
|
||||
import (
|
||||
// "errors"
|
||||
"fmt"
|
||||
seelog "github.com/cihub/seelog"
|
||||
// "io"
|
||||
)
|
||||
|
||||
var Logger seelog.LoggerInterface
|
||||
|
||||
func loadAppConfig() {
|
||||
appConfig := `
|
||||
<seelog minlevel="warn">
|
||||
<outputs formatid="common">
|
||||
<rollingfile type="size" filename="/data/logs/roll.log" maxsize="100000" maxrolls="5"/>
|
||||
<filter levels="critical">
|
||||
<file path="/data/logs/critical.log" formatid="critical"/>
|
||||
<smtp formatid="criticalemail" senderaddress="astaxie@gmail.com" sendername="ShortUrl API" hostname="smtp.gmail.com" hostport="587" username="mailusername" password="mailpassword">
|
||||
<recipient address="xiemengjun@gmail.com"/>
|
||||
</smtp>
|
||||
</filter>
|
||||
</outputs>
|
||||
<formats>
|
||||
<format id="common" format="%Date/%Time [%LEV] %Msg%n" />
|
||||
<format id="critical" format="%File %FullPath %Func %Msg%n" />
|
||||
<format id="criticalemail" format="Critical error on our server!\n %Time %Date %RelFile %Func %Msg \nSent by Seelog"/>
|
||||
</formats>
|
||||
</seelog>
|
||||
`
|
||||
logger, err := seelog.LoggerFromConfigAsBytes([]byte(appConfig))
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
UseLogger(logger)
|
||||
}
|
||||
|
||||
func init() {
|
||||
DisableLog()
|
||||
loadAppConfig()
|
||||
}
|
||||
|
||||
// DisableLog disables all library log output
|
||||
func DisableLog() {
|
||||
Logger = seelog.Disabled
|
||||
}
|
||||
|
||||
// UseLogger uses a specified seelog.LoggerInterface to output library log.
|
||||
// Use this func if you are using Seelog logging system in your app.
|
||||
func UseLogger(newLogger seelog.LoggerInterface) {
|
||||
Logger = newLogger
|
||||
}
|
||||
```
|
||||
上面主要实现了三个函数,
|
||||
|
||||
@@ -198,36 +198,36 @@ seelog支持自定义日志处理,下面是我基于它自定义的日志处
|
||||
上面我们定义了一个自定义的日志处理包,下面就是使用示例:
|
||||
```Go
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"project/logs"
|
||||
"project/configs"
|
||||
"project/routes"
|
||||
)
|
||||
|
||||
func main() {
|
||||
addr, _ := configs.MainConfig.String("server", "addr")
|
||||
logs.Logger.Info("Start server at:%v", addr)
|
||||
err := http.ListenAndServe(addr, routes.NewMux())
|
||||
logs.Logger.Critical("Server err:%v", err)
|
||||
}
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"project/logs"
|
||||
"project/configs"
|
||||
"project/routes"
|
||||
)
|
||||
|
||||
func main() {
|
||||
addr, _ := configs.MainConfig.String("server", "addr")
|
||||
logs.Logger.Info("Start server at:%v", addr)
|
||||
err := http.ListenAndServe(addr, routes.NewMux())
|
||||
logs.Logger.Critical("Server err:%v", err)
|
||||
}
|
||||
```
|
||||
## 发生错误发送邮件
|
||||
上面的例子解释了如何设置发送邮件,我们通过如下的smtp配置用来发送邮件:
|
||||
```html
|
||||
|
||||
<smtp formatid="criticalemail" senderaddress="astaxie@gmail.com" sendername="ShortUrl API" hostname="smtp.gmail.com" hostport="587" username="mailusername" password="mailpassword">
|
||||
<recipient address="xiemengjun@gmail.com"/>
|
||||
</smtp>
|
||||
<smtp formatid="criticalemail" senderaddress="astaxie@gmail.com" sendername="ShortUrl API" hostname="smtp.gmail.com" hostport="587" username="mailusername" password="mailpassword">
|
||||
<recipient address="xiemengjun@gmail.com"/>
|
||||
</smtp>
|
||||
```
|
||||
邮件的格式通过criticalemail配置,然后通过其他的配置发送邮件服务器的配置,通过recipient配置接收邮件的用户,如果有多个用户可以再添加一行。
|
||||
|
||||
要测试这个代码是否正常工作,可以在代码中增加类似下面的一个假消息。不过记住过后要把它删除,否则上线之后就会收到很多垃圾邮件。
|
||||
```Go
|
||||
|
||||
logs.Logger.Critical("test Critical message")
|
||||
logs.Logger.Critical("test Critical message")
|
||||
```
|
||||
现在,只要我们的应用在线上记录一个Critical的信息,你的邮箱就会收到一个Email,这样一旦线上的系统出现问题,你就能立马通过邮件获知,就能及时的进行处理。
|
||||
## 使用应用日志
|
||||
@@ -236,8 +236,8 @@ seelog支持自定义日志处理,下面是我基于它自定义的日志处
|
||||
举一个例子,我们需要跟踪用户尝试登陆系统的操作。这里会把成功与不成功的尝试都记录下来。记录成功的使用"Info"日志级别,而不成功的使用"warn"级别。如果想查找所有不成功的登陆,我们可以利用linux的grep之类的命令工具,如下:
|
||||
```Go
|
||||
|
||||
# cat /data/logs/roll.log | grep "failed login"
|
||||
2012-12-11 11:12:00 WARN : failed login attempt from 11.22.33.44 username password
|
||||
# cat /data/logs/roll.log | grep "failed login"
|
||||
2012-12-11 11:12:00 WARN : failed login attempt from 11.22.33.44 username password
|
||||
```
|
||||
通过这种方式我们就可以很方便的查找相应的信息,这样有利于我们针对应用日志做一些统计和分析。另外我们还需要考虑日志的大小,对于一个高流量的Web应用来说,日志的增长是相当可怕的,所以我们在seelog的配置文件里面设置了logrotate,这样就能保证日志文件不会因为不断变大而导致我们的磁盘空间不够引起问题。
|
||||
|
||||
|
||||
Reference in New Issue
Block a user