• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..11-Nov-2021-

.gitignoreH A D11-Nov-2021341 3526

.travis.ymlH A D11-Nov-2021102 1510

LICENSEH A D11-Nov-20211.1 KiB2217

MakefileH A D11-Nov-2021440 2015

README.mdH A D11-Nov-20214.4 KiB11792

formatter.goH A D11-Nov-202111.5 KiB431335

README.md

1# Logrus Prefixed Log Formatter
2[![Build Status](https://travis-ci.org/x-cray/logrus-prefixed-formatter.svg?branch=master)](https://travis-ci.org/x-cray/logrus-prefixed-formatter)
3
4[Logrus](https://github.com/sirupsen/logrus) formatter mainly based on original `logrus.TextFormatter` but with slightly
5modified colored output and support for log entry prefixes, e.g. message source followed by a colon. In addition, custom
6color themes are supported.
7
8![Formatter screenshot](http://cl.ly/image/1w0B3F233F3z/formatter-screenshot@2x.png)
9
10Just like with the original `logrus.TextFormatter` when a TTY is not attached, the output is compatible with the
11[logfmt](http://godoc.org/github.com/kr/logfmt) format:
12
13```text
14time="Oct 27 00:44:26" level=debug msg="Started observing beach" animal=walrus number=8
15time="Oct 27 00:44:26" level=info msg="A group of walrus emerges from the ocean" animal=walrus size=10
16time="Oct 27 00:44:26" level=warning msg="The group's number increased tremendously!" number=122 omg=true
17time="Oct 27 00:44:26" level=debug msg="Temperature changes" temperature=-4
18time="Oct 27 00:44:26" level=panic msg="It's over 9000!" animal=orca size=9009
19time="Oct 27 00:44:26" level=fatal msg="The ice breaks!" number=100 omg=true
20exit status 1
21```
22
23## Installation
24To install formatter, use `go get`:
25
26```sh
27$ go get github.com/x-cray/logrus-prefixed-formatter
28```
29
30## Usage
31Here is how it should be used:
32
33```go
34package main
35
36import (
37	"github.com/sirupsen/logrus"
38	prefixed "github.com/x-cray/logrus-prefixed-formatter"
39)
40
41var log = logrus.New()
42
43func init() {
44	log.Formatter = new(prefixed.TextFormatter)
45	log.Level = logrus.DebugLevel
46}
47
48func main() {
49	log.WithFields(logrus.Fields{
50		"prefix": "main",
51		"animal": "walrus",
52		"number": 8,
53	}).Debug("Started observing beach")
54
55	log.WithFields(logrus.Fields{
56		"prefix":      "sensor",
57		"temperature": -4,
58	}).Info("Temperature changes")
59}
60```
61
62## API
63`prefixed.TextFormatter` exposes the following fields and methods.
64
65### Fields
66
67* `ForceColors bool` — set to true to bypass checking for a TTY before outputting colors.
68* `DisableColors bool` — force disabling colors. For a TTY colors are enabled by default.
69* `DisableUppercase bool` — set to true to turn off the conversion of the log level names to uppercase.
70* `ForceFormatting bool` — force formatted layout, even for non-TTY output.
71* `DisableTimestamp bool` — disable timestamp logging. Useful when output is redirected to logging system that already adds timestamps.
72* `FullTimestamp bool` — enable logging the full timestamp when a TTY is attached instead of just the time passed since beginning of execution.
73* `TimestampFormat string` — timestamp format to use for display when a full timestamp is printed.
74* `DisableSorting bool` — the fields are sorted by default for a consistent output. For applications that log extremely frequently and don't use the JSON formatter this may not be desired.
75* `QuoteEmptyFields bool` — wrap empty fields in quotes if true.
76* `QuoteCharacter string` — can be set to the override the default quoting character `"` with something else. For example: `'`, or `` ` ``.
77* `SpacePadding int` — pad msg field with spaces on the right for display. The value for this parameter will be the size of padding. Its default value is zero, which means no padding will be applied.
78
79### Methods
80
81#### `SetColorScheme(colorScheme *prefixed.ColorScheme)`
82
83Sets an alternative color scheme for colored output. `prefixed.ColorScheme` struct supports the following fields:
84* `InfoLevelStyle string` — info level style.
85* `WarnLevelStyle string` — warn level style.
86* `ErrorLevelStyle string` — error style.
87* `FatalLevelStyle string` — fatal level style.
88* `PanicLevelStyle string` — panic level style.
89* `DebugLevelStyle string` — debug level style.
90* `PrefixStyle string` — prefix style.
91* `TimestampStyle string` — timestamp style.
92
93Color styles should be specified using [mgutz/ansi](https://github.com/mgutz/ansi#style-format) style syntax. For example, here is the default theme:
94
95```go
96InfoLevelStyle:  "green",
97WarnLevelStyle:  "yellow",
98ErrorLevelStyle: "red",
99FatalLevelStyle: "red",
100PanicLevelStyle: "red",
101DebugLevelStyle: "blue",
102PrefixStyle:     "cyan",
103TimestampStyle:  "black+h"
104```
105
106It's not necessary to specify all colors when changing color scheme if you want to change just specific ones:
107
108```go
109formatter.SetColorScheme(&prefixed.ColorScheme{
110    PrefixStyle:    "blue+b",
111    TimestampStyle: "white+h",
112})
113```
114
115# License
116MIT
117