| 1 |
# Logrus Prefixed Log Formatter |
| 2 |
[Logrus](https://github.com/Sirupsen/logrus) formatter mainly based on original `logrus.TextFormatter` but with slightly |
| 3 |
modified colored output and support for log entry prefixes, e.g. message source followed by a colon. |
| 4 |
|
| 5 |
 |
| 6 |
|
| 7 |
Just like with the original `logrus.TextFormatter` when a TTY is not attached, the output is compatible with the |
| 8 |
[logfmt](http://godoc.org/github.com/kr/logfmt) format: |
| 9 |
|
| 10 |
```text |
| 11 |
time="Oct 27 00:44:26" level=debug msg="Started observing beach" animal=walrus number=8 |
| 12 |
time="Oct 27 00:44:26" level=info msg="A group of walrus emerges from the ocean" animal=walrus size=10 |
| 13 |
time="Oct 27 00:44:26" level=warning msg="The group's number increased tremendously!" number=122 omg=true |
| 14 |
time="Oct 27 00:44:26" level=debug msg="Temperature changes" temperature=-4 |
| 15 |
time="Oct 27 00:44:26" level=panic msg="It's over 9000!" animal=orca size=9009 |
| 16 |
time="Oct 27 00:44:26" level=fatal msg="The ice breaks!" number=100 omg=true |
| 17 |
exit status 1 |
| 18 |
``` |
| 19 |
|
| 20 |
## Installation |
| 21 |
To install formatter, use `go get`: |
| 22 |
|
| 23 |
```sh |
| 24 |
$ go get github.com/x-cray/logrus-prefixed-formatter |
| 25 |
``` |
| 26 |
|
| 27 |
## Usage |
| 28 |
Here is how it should be used: |
| 29 |
|
| 30 |
```go |
| 31 |
package main |
| 32 |
|
| 33 |
import ( |
| 34 |
"github.com/Sirupsen/logrus" |
| 35 |
prefixed "github.com/x-cray/logrus-prefixed-formatter" |
| 36 |
) |
| 37 |
|
| 38 |
var log = logrus.New() |
| 39 |
|
| 40 |
func init() { |
| 41 |
log.Formatter = new(prefixed.TextFormatter) |
| 42 |
log.Level = logrus.DebugLevel |
| 43 |
} |
| 44 |
|
| 45 |
func main() { |
| 46 |
log.WithFields(logrus.Fields{ |
| 47 |
"prefix": "main", |
| 48 |
"animal": "walrus", |
| 49 |
"number": 8, |
| 50 |
}).Debug("Started observing beach") |
| 51 |
|
| 52 |
log.WithFields(logrus.Fields{ |
| 53 |
"prefix": "sensor", |
| 54 |
"temperature": -4, |
| 55 |
}).Info("Temperature changes") |
| 56 |
} |
| 57 |
``` |
| 58 |
|
| 59 |
## API |
| 60 |
`prefixed.TextFormatter` exposes the following fields: |
| 61 |
|
| 62 |
* `ForceColors bool` — set to true to bypass checking for a TTY before outputting colors. |
| 63 |
* `DisableColors bool` — force disabling colors. |
| 64 |
* `DisableTimestamp bool` — disable timestamp logging. useful when output is redirected to logging system that already adds timestamps. |
| 65 |
* `ShortTimestamp bool` — enable logging of just the time passed since beginning of execution. |
| 66 |
* `TimestampFormat string` — timestamp format to use for display when a full timestamp is printed. |
| 67 |
* `DisableSorting bool` — the fields are sorted by default for a consistent output. For applications |
| 68 |
that log extremely frequently and don't use the JSON formatter this may not be desired. |
| 69 |
|
| 70 |
# License |
| 71 |
MIT |