# Logrus <img src="http://i.imgur.com/hTeVwmJ.png" width="40" height="40" alt=":walrus:" class="emoji" title=":walrus:"/> [![Build Status](https://travis-ci.org/Sirupsen/logrus.svg?branch=master)](https://travis-ci.org/Sirupsen/logrus) [![GoDoc](https://godoc.org/github.com/Sirupsen/logrus?status.svg)](https://godoc.org/github.com/Sirupsen/logrus)
Logrus is a structured logger for Go (golang), completely API compatible with
the standard library logger. [Godoc][godoc]. **Please note the Logrus API is not
yet stable (pre 1.0). Logrus itself is completely stable and has been used in
many large deployments. The core API is unlikely to change much but please
version control your Logrus to make sure you aren't fetching latest `master` on
every build.**
Nicely color-coded in development (when a TTY is attached, otherwise just
plain text):
![Colored](http://i.imgur.com/PY7qMwd.png)
With `log.SetFormatter(&log.JSONFormatter{})`, for easy parsing by logstash
or Splunk:
```json
{"animal":"walrus","level":"info","msg":"A group of walrus emerges from the
ocean","size":10,"time":"2014-03-10 19:57:38.562264131 -0400 EDT"}
{"level":"warning","msg":"The group's number increased tremendously!",
"number":122,"omg":true,"time":"2014-03-10 19:57:38.562471297 -0400 EDT"}
{"animal":"walrus","level":"info","msg":"A giant walrus appears!",
"size":10,"time":"2014-03-10 19:57:38.562500591 -0400 EDT"}
{"animal":"walrus","level":"info","msg":"Tremendously sized cow enters the ocean.",
"size":9,"time":"2014-03-10 19:57:38.562527896 -0400 EDT"}
{"level":"fatal","msg":"The ice breaks!","number":100,"omg":true,
"time":"2014-03-10 19:57:38.562543128 -0400 EDT"}
```
With the default `log.SetFormatter(&log.TextFormatter{})` when a TTY is not
attached, the output is compatible with the
[logfmt](http://godoc.org/github.com/kr/logfmt) format:
```text
time="2015-03-26T01:27:38-04:00" level=debug msg="Started observing beach" animal=walrus number=8
time="2015-03-26T01:27:38-04:00" level=info msg="A group of walrus emerges from the ocean" animal=walrus size=10
time="2015-03-26T01:27:38-04:00" level=warning msg="The group's number increased tremendously!" number=122 omg=true
time="2015-03-26T01:27:38-04:00" level=debug msg="Temperature changes" temperature=-4
time="2015-03-26T01:27:38-04:00" level=panic msg="It's over 9000!" animal=orca size=9009
time="2015-03-26T01:27:38-04:00" level=fatal msg="The ice breaks!" err=&{0x2082280c0 map[animal:orca size:9009] 2015-03-26 01:27:38.441574009 -0400 EDT panic It's over 9000!} number=100 omg=true
exit status 1
```
#### Example
The simplest way to use Logrus is simply the package-level exported logger:
```go
package main
import (
log "github.com/Sirupsen/logrus"
)
func main() {
log.WithFields(log.Fields{
"animal": "walrus",
}).Info("A walrus appears")
}
```
Note that it's completely api-compatible with the stdlib logger, so you can
replace your `log` imports everywhere with `log "github.com/Sirupsen/logrus"`
and you'll now have the flexibility of Logrus. You can customize it all you
want:
```go
package main
import (
"os"
log "github.com/Sirupsen/logrus"
)
func init() {
// Log as JSON instead of the default ASCII formatter.
log.SetFormatter(&log.JSONFormatter{})
// Output to stderr instead of stdout, could also be a file.
log.SetOutput(os.Stderr)
// Only log the warning severity or above.
log.SetLevel(log.WarnLevel)
}
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": 100,
}).Fatal("The ice breaks!")
// A common pattern is to re-use fields between logging statements by re-using
// the logrus.Entry returned from 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")
}
```
For more advanced usage such as logging to multiple locations from the same
application, you can also create an instance of the `logrus` Logger:
```go
package main
import (
"github.com/Sirupsen/logrus"
)
// Create a new instance of the logger. You can have any number of instances.
var log = logrus.New()
func main() {
// The API for setting attributes is a little different than the package level
// exported logger. See Godoc.
log.Out = os.Stderr
log.WithFields(logrus.Fields{
"animal": "walrus",
"size": 10,
}).Info("A group of walrus emerges from the ocean")
}
```
#### Fields
Logrus encourages careful, structured logging though logging fields instead of
long, unparseable error messages. For example, instead of: `log.Fatalf("Failed
to send event %s to topic %s with key %d")`, you should log the much more
discoverable:
```go
log.WithFields(log.Fields{
"event": event,
"topic": topic,
"key": key,
}).Fatal("Failed to send event")
```
We've found this API forces you to think about logging in a way that produces
much more useful logging messages. We've been in countless situations where just
a single added field to a log statement that was already there would've saved us
hours. The `WithFields` call is optional.
In general, with Logrus using any of the `printf`-family functions should be
seen as a hint you should add a field, however, you can still use the
`printf`-family functions with Logrus.
#### Hooks
You can add hooks for logging levels. For example to send errors to an exception
tracking service on `Error`, `Fatal` and `Panic`, info to StatsD or log to
multiple places simultaneously, e.g. syslog.
Logrus comes with [built-in hooks](hooks/). Add those, or your custom hook, in
`init`:
```go
import (
log "github.com/Sirupsen/logrus"
"gopkg.in/gemnasium/logrus-airbrake-hook.v2" // the package is named "aibrake"
logrus_syslog "github.com/Sirupsen/logrus/hooks/syslog"
"log/syslog"
)
func init() {
// Use the Airbrake hook to report errors that have Error severity or above to
// an exception tracker. You can create custom hooks, see the Hooks section.
log.AddHook(airbrake.NewHook(123, "xyz", "production"))
hook, err := logrus_syslog.NewSyslogHook("udp", "localhost:514", syslog.LOG_INFO, "")
if err != nil {
log.Error("Unable to connect to local syslog daemon")
} else {
log.AddHook(hook)
}
}
```
Note: Syslog hook also support connecting to local syslog (Ex. "/dev/log" or "/var/run/syslog" or "/var/run/log"). For the detail, please check the [syslog hook README](hooks/syslog/README.md).
| Hook | Description |
| ----- | ----------- |
| [Airbrake](https://github.com/gemnasium/logrus-airbrake-hook) | Send errors to the Airbrake API V3. Uses the official [`gobrake`](https://github.com/airbrake/gobrake) behind the scenes. |
| [Airbrake "legacy"](https://github.com/gemnasium/logrus-airbrake-legacy-hook) | Send errors to an exception tracking service compatible with the Airbrake API V2. Uses [`airbrake-go`](https://github.com/tobi/airbrake-go) behind the scenes. |
| [Papertrail](https://github.com/polds/logrus-papertrail-hook) | Send errors to the [Papertrail](https://papertrailapp.com) hosted logging service via UDP. |
| [Syslog](https://github.com/Sirupsen/logrus/blob/master/hooks/syslog/syslog.go) | Send errors to remote syslog server. Uses standard library `log/syslog` behind the scenes. |
| [Bugsnag](https://github.com/Shopify/logrus-bugsnag/blob/master/bugsnag.go) | Send errors to the Bugsnag exception tracking service. |
| [Sentry](https://github.com/evalphobia/logrus_sentry) | Send errors to the Sentry error logging and aggregation service. |
| [Hiprus](https://github.com/nubo/hiprus) | Send errors to a channel in hipchat. |
| [Logrusly](https://github.com/sebest/logrusly) | Send logs to [Loggly
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
Go并发编程实战,代码学习 (2144个子文件)
v0.3.0 20KB
v0.2.0 19KB
v0.1.0 18KB
00191335b308b6da291deda63c42b8772e56c1 2KB
00df4bc909e813d51c2bfb78cfe8d85433a656 855B
00e3c282db417d4fbfb455f990eab7416d3531 1KB
025ab556dbe2c3f7e11924023fd0b3a8b35676 268B
0381cd26ae7637fa84026ecc802e2cb3c063af 134B
03e30dd1711cc86772530bfc318314c37d0da0 663B
03f08695eb644a8df2643289ef07429f9b9d4e 39B
04aa2d30bac210a5454106d68aef718f2af78e 4KB
04cb1255941c90fdfa09ac8615de5729fb4adf 727B
0517c9f347160546520eb59b4936e867cf083d 55B
0544ea24754fafee635c12cde08fd1cb1daed0 462B
05562929707d27e282569e819bacb4c9526601 1KB
05ba3102f63aeb5cdb7e871dbbb1ee7b7f3e34 8KB
05c1a5a05c11041a314df9bac6e3ba6da98e24 1KB
05d4b4a778cc65f29d0a679a91352d618ee360 928B
05ef7c20c451052723c63854fd492f36d627d6 965B
0600ccbdfd9c7b0420f0825bc45aee3f202af9 18KB
0680373d5557856ff17ae7c25de3f61d1a0a45 918B
06ed7730f75e4fcdb7fa6c28524d6b6f3a6149 551B
073b7810b8dcc0340db720af2a83c9b0195db2 617B
0792ea93b5abf5c82286e7fd80826039d1e95c 243B
07e95158eae25debf22a2f7f1103655e2c0264 1KB
085a072903140c62562bd67d82580853c7b1e4 707B
08659859020fdd5907c5a4b7ffc5febd8abd44 241B
0866a30771d894aa3c74d93116b1203cfa6b5a 4KB
087f22f3e9484988b8f6cfd67b9f5c8995f626 58B
08b25a58bfb37e72dc2c23937fa3025aa8b375 3KB
099b9f937f861034e13ed4170925ac43ddf30f 224B
09bdd701fc0600ace6e79aa0b2163d3ba93511 4KB
0a11010240431b5e0494334727e03feb41ab5d 94B
0a132a33a53cb6eb1a5fcefc7de14d5d9a80f9 655B
0a3d88f44344c6b96e894ecbfc4ebc243b362e 111B
0a4697961fa02d84c63d2ac443c0d20f341de5 2KB
0a924eafd0350706f975a10c8951528f960c8a 210B
0b2cdbc23feb92f64e2ce12dcb723ba9de89c9 3KB
0b57465822d141765306422924858e15d8835c 355B
0c04c60e95fc4d57d09119b386abf23c9d8645 20KB
0cd804d88275ee6f6107b9931ef90c8a2c4ddb 730B
0cf6a3027726007f348c5ede9a74211fe7b022 731B
0d0299b5cf91afaa536469e96a1ffdd9c0614b 18KB
0d5c8da083685291598050afc2b2555c0e6e11 1KB
0d8f988230b34128c371d25f6985664208f664 7KB
0dc81696c7bedfe73322c92bc3f0b78835b1f3 6KB
0dfae319a42253d239a9ad86512f87e83cd229 4KB
0e1e447d628d4e81f460c7bf282ed4babdbcd6 481B
0e1f068a650d6dcee7c97c834c5cb506523328 537B
0e50e037214c3c264076f85ea8548e8add9dd0 286B
0ebbf2b23ca44388df7f6db0f31eeb6dd60882 13KB
0ef77f86ef7255cd04a6e1b0b93e0553fc435f 365B
0f12234adf847a915f13ff69d9048d7d3b4d73 25B
0f90f1bc9f09fcca606f90acc30b3fcfe3239d 436B
v0.1.1 18KB
10318faa6bf68743cd292067f7c8613db4f7ff 60B
1034a345d1fa34f0a1254966f4b740ac11620a 4KB
10680042ccfd7a250c8fe2fca84e4ef7293fbf 57B
107d277b6a7f1597155ad2527868bbc2c323de 423B
10b7c575fe5d7bfb429ef10030b509a60f6b04 739B
10c94888a67647c3320f359ab2e35b0628dbd0 514B
10e88eddee07d3e425b1dcc8a4b53bb5a62031 12KB
11a6a23b3f66de0dd500905b6e55002533059d 173B
11b3cd6057cb00227394f70a19874ed4846133 87B
11dc23deb7374c334568de768abdb1838ea098 654B
122604780c49837d0a23364edc4133d6dd8e25 49B
1265620d72c51e8ee3e8ebd1107dcf4ddd733c 1KB
1284312af732cfaaba3233c889d1311027b483 770B
130e424df8435cfaab5cb5882e9508f8a98e0b 352B
132721bdddabf93fbe2d7ceac1fa59cc1f1c9e 5KB
13d8e5050227a7fb0e94626fb16b1403b505a9 125B
13e448b7b4c3ec9214be744a2ea79bb33d2909 426B
140b0df6c1bdaba3da36248b756f1c881f136d 2KB
143084887f436a1f0bea4bdc4aefa9e526b09f 2KB
146a7802bc90cb30ad3e3d91ce40c0ef3a25ab 2KB
1473f1610c32235059e0566f9b0fdd36168345 3KB
14917488e8a8072e2df1805ffc0bfd8e92d4c0 915B
14b3ca478a8a246f9fe1e92f9a70d031300fab 1KB
151cdc39275a003d6a6e7059b0801071c001a8 570B
15ab3fca781d8909743db4bc2c7327d11dd506 284B
15fa0ee4f59b680eecd47a8cb4fc33b278b5e6 1KB
16162f0248af352c5adb2144ba066a47f9545b 302B
167cd746c560e5b3d3b233a169aa64d3e9101e 150B
169fa30d9ddaeb93e52ebfc02b92d58a8afc43 2KB
16a3fd7ef692f630b69a4e46ac52f1852d51ee 2KB
172477c8697ca290a4663765c79c06ac38761d 368B
1789d97e1477d60ef493e4e7848d3956f2f74a 50B
17a69d02c2c2fa7096ded25c4d5c1487576134 4KB
1837dd2af638a0a40526d3a0b175af8e13ebbe 991B
184cae0696edb6c14824904c73d3c99928bcc2 1KB
1933d9621f777cfe98833ce7eb95e3ba15c484 7KB
193c660bcac9e815344046edcca5212bd550c5 1KB
198155a79e5f8a9c488b9fadfde4cb55edc1b9 329B
1993ee6f422fe91c0acb355b28e6e8bec477e7 54B
19f3448228a1db0cd9d1a80a602d49ffd78970 5KB
19fde3763f542f760a487a07a0aacdf8dfaa83 2KB
1a15e75b71f6582abc1f4d033905319be51814 3KB
1a4d1ab18b9ce39297acb45c40e0be1b604e63 339B
1a5b8564faabe6702086169e9565b2887ca21d 4KB
1ab42a1a2a05d7c4fecc45a84a05bb252c94c8 601B
共 2144 条
- 1
- 2
- 3
- 4
- 5
- 6
- 22
资源评论
叶落-秋风凉
- 粉丝: 0
- 资源: 17
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功