aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/sirupsen/logrus
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/sirupsen/logrus')
-rw-r--r--vendor/github.com/sirupsen/logrus/.gitignore4
-rw-r--r--vendor/github.com/sirupsen/logrus/.golangci.yml40
-rw-r--r--vendor/github.com/sirupsen/logrus/.travis.yml15
-rw-r--r--vendor/github.com/sirupsen/logrus/CHANGELOG.md259
-rw-r--r--vendor/github.com/sirupsen/logrus/LICENSE21
-rw-r--r--vendor/github.com/sirupsen/logrus/README.md515
-rw-r--r--vendor/github.com/sirupsen/logrus/alt_exit.go76
-rw-r--r--vendor/github.com/sirupsen/logrus/appveyor.yml14
-rw-r--r--vendor/github.com/sirupsen/logrus/buffer_pool.go43
-rw-r--r--vendor/github.com/sirupsen/logrus/doc.go26
-rw-r--r--vendor/github.com/sirupsen/logrus/entry.go442
-rw-r--r--vendor/github.com/sirupsen/logrus/exported.go270
-rw-r--r--vendor/github.com/sirupsen/logrus/formatter.go78
-rw-r--r--vendor/github.com/sirupsen/logrus/hooks.go34
-rw-r--r--vendor/github.com/sirupsen/logrus/json_formatter.go128
-rw-r--r--vendor/github.com/sirupsen/logrus/logger.go417
-rw-r--r--vendor/github.com/sirupsen/logrus/logrus.go186
-rw-r--r--vendor/github.com/sirupsen/logrus/terminal_check_appengine.go11
-rw-r--r--vendor/github.com/sirupsen/logrus/terminal_check_bsd.go13
-rw-r--r--vendor/github.com/sirupsen/logrus/terminal_check_js.go7
-rw-r--r--vendor/github.com/sirupsen/logrus/terminal_check_no_terminal.go11
-rw-r--r--vendor/github.com/sirupsen/logrus/terminal_check_notappengine.go17
-rw-r--r--vendor/github.com/sirupsen/logrus/terminal_check_solaris.go11
-rw-r--r--vendor/github.com/sirupsen/logrus/terminal_check_unix.go13
-rw-r--r--vendor/github.com/sirupsen/logrus/terminal_check_windows.go27
-rw-r--r--vendor/github.com/sirupsen/logrus/text_formatter.go339
-rw-r--r--vendor/github.com/sirupsen/logrus/writer.go102
27 files changed, 3119 insertions, 0 deletions
diff --git a/vendor/github.com/sirupsen/logrus/.gitignore b/vendor/github.com/sirupsen/logrus/.gitignore
new file mode 100644
index 0000000..1fb13ab
--- /dev/null
+++ b/vendor/github.com/sirupsen/logrus/.gitignore
@@ -0,0 +1,4 @@
1logrus
2vendor
3
4.idea/
diff --git a/vendor/github.com/sirupsen/logrus/.golangci.yml b/vendor/github.com/sirupsen/logrus/.golangci.yml
new file mode 100644
index 0000000..65dc285
--- /dev/null
+++ b/vendor/github.com/sirupsen/logrus/.golangci.yml
@@ -0,0 +1,40 @@
1run:
2 # do not run on test files yet
3 tests: false
4
5# all available settings of specific linters
6linters-settings:
7 errcheck:
8 # report about not checking of errors in type assetions: `a := b.(MyStruct)`;
9 # default is false: such cases aren't reported by default.
10 check-type-assertions: false
11
12 # report about assignment of errors to blank identifier: `num, _ := strconv.Atoi(numStr)`;
13 # default is false: such cases aren't reported by default.
14 check-blank: false
15
16 lll:
17 line-length: 100
18 tab-width: 4
19
20 prealloc:
21 simple: false
22 range-loops: false
23 for-loops: false
24
25 whitespace:
26 multi-if: false # Enforces newlines (or comments) after every multi-line if statement
27 multi-func: false # Enforces newlines (or comments) after every multi-line function signature
28
29linters:
30 enable:
31 - megacheck
32 - govet
33 disable:
34 - maligned
35 - prealloc
36 disable-all: false
37 presets:
38 - bugs
39 - unused
40 fast: false
diff --git a/vendor/github.com/sirupsen/logrus/.travis.yml b/vendor/github.com/sirupsen/logrus/.travis.yml
new file mode 100644
index 0000000..c1dbd5a
--- /dev/null
+++ b/vendor/github.com/sirupsen/logrus/.travis.yml
@@ -0,0 +1,15 @@
1language: go
2go_import_path: github.com/sirupsen/logrus
3git:
4 depth: 1
5env:
6 - GO111MODULE=on
7go: 1.15.x
8os: linux
9install:
10 - ./travis/install.sh
11script:
12 - cd ci
13 - go run mage.go -v -w ../ crossBuild
14 - go run mage.go -v -w ../ lint
15 - go run mage.go -v -w ../ test
diff --git a/vendor/github.com/sirupsen/logrus/CHANGELOG.md b/vendor/github.com/sirupsen/logrus/CHANGELOG.md
new file mode 100644
index 0000000..7567f61
--- /dev/null
+++ b/vendor/github.com/sirupsen/logrus/CHANGELOG.md
@@ -0,0 +1,259 @@
1# 1.8.1
2Code quality:
3 * move magefile in its own subdir/submodule to remove magefile dependency on logrus consumer
4 * improve timestamp format documentation
5
6Fixes:
7 * fix race condition on logger hooks
8
9
10# 1.8.0
11
12Correct versioning number replacing v1.7.1.
13
14# 1.7.1
15
16Beware this release has introduced a new public API and its semver is therefore incorrect.
17
18Code quality:
19 * use go 1.15 in travis
20 * use magefile as task runner
21
22Fixes:
23 * small fixes about new go 1.13 error formatting system
24 * Fix for long time race condiction with mutating data hooks
25
26Features:
27 * build support for zos
28
29# 1.7.0
30Fixes:
31 * the dependency toward a windows terminal library has been removed
32
33Features:
34 * a new buffer pool management API has been added
35 * a set of `<LogLevel>Fn()` functions have been added
36
37# 1.6.0
38Fixes:
39 * end of line cleanup
40 * revert the entry concurrency bug fix whic leads to deadlock under some circumstances
41 * update dependency on go-windows-terminal-sequences to fix a crash with go 1.14
42
43Features:
44 * add an option to the `TextFormatter` to completely disable fields quoting
45
46# 1.5.0
47Code quality:
48 * add golangci linter run on travis
49
50Fixes:
51 * add mutex for hooks concurrent access on `Entry` data
52 * caller function field for go1.14
53 * fix build issue for gopherjs target
54
55Feature:
56 * add an hooks/writer sub-package whose goal is to split output on different stream depending on the trace level
57 * add a `DisableHTMLEscape` option in the `JSONFormatter`
58 * add `ForceQuote` and `PadLevelText` options in the `TextFormatter`
59
60# 1.4.2
61 * Fixes build break for plan9, nacl, solaris
62# 1.4.1
63This new release introduces:
64 * Enhance TextFormatter to not print caller information when they are empty (#944)
65 * Remove dependency on golang.org/x/crypto (#932, #943)
66
67Fixes:
68 * Fix Entry.WithContext method to return a copy of the initial entry (#941)
69
70# 1.4.0
71This new release introduces:
72 * Add `DeferExitHandler`, similar to `RegisterExitHandler` but prepending the handler to the list of handlers (semantically like `defer`) (#848).
73 * Add `CallerPrettyfier` to `JSONFormatter` and `TextFormatter` (#909, #911)
74 * Add `Entry.WithContext()` and `Entry.Context`, to set a context on entries to be used e.g. in hooks (#919).
75
76Fixes:
77 * Fix wrong method calls `Logger.Print` and `Logger.Warningln` (#893).
78 * Update `Entry.Logf` to not do string formatting unless the log level is enabled (#903)
79 * Fix infinite recursion on unknown `Level.String()` (#907)
80 * Fix race condition in `getCaller` (#916).
81
82
83# 1.3.0
84This new release introduces:
85 * Log, Logf, Logln functions for Logger and Entry that take a Level
86
87Fixes:
88 * Building prometheus node_exporter on AIX (#840)
89 * Race condition in TextFormatter (#468)
90 * Travis CI import path (#868)
91 * Remove coloured output on Windows (#862)
92 * Pointer to func as field in JSONFormatter (#870)
93 * Properly marshal Levels (#873)
94
95# 1.2.0
96This new release introduces:
97 * A new method `SetReportCaller` in the `Logger` to enable the file, line and calling function from which the trace has been issued
98 * A new trace level named `Trace` whose level is below `Debug`
99 * A configurable exit function to be called upon a Fatal trace
100 * The `Level` object now implements `encoding.TextUnmarshaler` interface
101
102# 1.1.1
103This is a bug fix release.
104 * fix the build break on Solaris
105 * don't drop a whole trace in JSONFormatter when a field param is a function pointer which can not be serialized
106
107# 1.1.0
108This new release introduces:
109 * several fixes:
110 * a fix for a race condition on entry formatting
111 * proper cleanup of previously used entries before putting them back in the pool
112 * the extra new line at the end of message in text formatter has been removed
113 * a new global public API to check if a level is activated: IsLevelEnabled
114 * the following methods have been added to the Logger object
115 * IsLevelEnabled
116 * SetFormatter
117 * SetOutput
118 * ReplaceHooks
119 * introduction of go module
120 * an indent configuration for the json formatter
121 * output colour support for windows
122 * the field sort function is now configurable for text formatter
123 * the CLICOLOR and CLICOLOR\_FORCE environment variable support in text formater
124
125# 1.0.6
126
127This new release introduces:
128 * a new api WithTime which allows to easily force the time of the log entry
129 which is mostly useful for logger wrapper
130 * a fix reverting the immutability of the entry given as parameter to the hooks
131 a new configuration field of the json formatter in order to put all the fields
132 in a nested dictionnary
133 * a new SetOutput method in the Logger
134 * a new configuration of the textformatter to configure the name of the default keys
135 * a new configuration of the text formatter to disable the level truncation
136
137# 1.0.5
138
139* Fix hooks race (#707)
140* Fix panic deadlock (#695)
141
142# 1.0.4
143
144* Fix race when adding hooks (#612)
145* Fix terminal check in AppEngine (#635)
146
147# 1.0.3
148
149* Replace example files with testable examples
150
151# 1.0.2
152
153* bug: quote non-string values in text formatter (#583)
154* Make (*Logger) SetLevel a public method
155
156# 1.0.1
157
158* bug: fix escaping in text formatter (#575)
159
160# 1.0.0
161
162* Officially changed name to lower-case
163* bug: colors on Windows 10 (#541)
164* bug: fix race in accessing level (#512)
165
166# 0.11.5
167
168* feature: add writer and writerlevel to entry (#372)
169
170# 0.11.4
171
172* bug: fix undefined variable on solaris (#493)
173
174# 0.11.3
175
176* formatter: configure quoting of empty values (#484)
177* formatter: configure quoting character (default is `"`) (#484)
178* bug: fix not importing io correctly in non-linux environments (#481)
179
180# 0.11.2
181
182* bug: fix windows terminal detection (#476)
183
184# 0.11.1
185
186* bug: fix tty detection with custom out (#471)
187
188# 0.11.0
189
190* performance: Use bufferpool to allocate (#370)
191* terminal: terminal detection for app-engine (#343)
192* feature: exit handler (#375)
193
194# 0.10.0
195
196* feature: Add a test hook (#180)
197* feature: `ParseLevel` is now case-insensitive (#326)
198* feature: `FieldLogger` interface that generalizes `Logger` and `Entry` (#308)
199* performance: avoid re-allocations on `WithFields` (#335)
200
201# 0.9.0
202
203* logrus/text_formatter: don't emit empty msg
204* logrus/hooks/airbrake: move out of main repository
205* logrus/hooks/sentry: move out of main repository
206* logrus/hooks/papertrail: move out of main repository
207* logrus/hooks/bugsnag: move out of main repository
208* logrus/core: run tests with `-race`
209* logrus/core: detect TTY based on `stderr`
210* logrus/core: support `WithError` on logger
211* logrus/core: Solaris support
212
213# 0.8.7
214
215* logrus/core: fix possible race (#216)
216* logrus/doc: small typo fixes and doc improvements
217
218
219# 0.8.6
220
221* hooks/raven: allow passing an initialized client
222
223# 0.8.5
224
225* logrus/core: revert #208
226
227# 0.8.4
228
229* formatter/text: fix data race (#218)
230
231# 0.8.3
232
233* logrus/core: fix entry log level (#208)
234* logrus/core: improve performance of text formatter by 40%
235* logrus/core: expose `LevelHooks` type
236* logrus/core: add support for DragonflyBSD and NetBSD
237* formatter/text: print structs more verbosely
238
239# 0.8.2
240
241* logrus: fix more Fatal family functions
242
243# 0.8.1
244
245* logrus: fix not exiting on `Fatalf` and `Fatalln`
246
247# 0.8.0
248
249* logrus: defaults to stderr instead of stdout
250* hooks/sentry: add special field for `*http.Request`
251* formatter/text: ignore Windows for colors
252
253# 0.7.3
254
255* formatter/\*: allow configuration of timestamp layout
256
257# 0.7.2
258
259* formatter/text: Add configuration option for time format (#158)
diff --git a/vendor/github.com/sirupsen/logrus/LICENSE b/vendor/github.com/sirupsen/logrus/LICENSE
new file mode 100644
index 0000000..f090cb4
--- /dev/null
+++ b/vendor/github.com/sirupsen/logrus/LICENSE
@@ -0,0 +1,21 @@
1The MIT License (MIT)
2
3Copyright (c) 2014 Simon Eskildsen
4
5Permission is hereby granted, free of charge, to any person obtaining a copy
6of this software and associated documentation files (the "Software"), to deal
7in the Software without restriction, including without limitation the rights
8to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9copies of the Software, and to permit persons to whom the Software is
10furnished to do so, subject to the following conditions:
11
12The above copyright notice and this permission notice shall be included in
13all copies or substantial portions of the Software.
14
15THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21THE SOFTWARE.
diff --git a/vendor/github.com/sirupsen/logrus/README.md b/vendor/github.com/sirupsen/logrus/README.md
new file mode 100644
index 0000000..d1d4a85
--- /dev/null
+++ b/vendor/github.com/sirupsen/logrus/README.md
@@ -0,0 +1,515 @@
1# Logrus <img src="http://i.imgur.com/hTeVwmJ.png" width="40" height="40" alt=":walrus:" class="emoji" title=":walrus:"/> [![Build Status](https://github.com/sirupsen/logrus/workflows/CI/badge.svg)](https://github.com/sirupsen/logrus/actions?query=workflow%3ACI) [![Build Status](https://travis-ci.org/sirupsen/logrus.svg?branch=master)](https://travis-ci.org/sirupsen/logrus) [![Go Reference](https://pkg.go.dev/badge/github.com/sirupsen/logrus.svg)](https://pkg.go.dev/github.com/sirupsen/logrus)
2
3Logrus is a structured logger for Go (golang), completely API compatible with
4the standard library logger.
5
6**Logrus is in maintenance-mode.** We will not be introducing new features. It's
7simply too hard to do in a way that won't break many people's projects, which is
8the last thing you want from your Logging library (again...).
9
10This does not mean Logrus is dead. Logrus will continue to be maintained for
11security, (backwards compatible) bug fixes, and performance (where we are
12limited by the interface).
13
14I believe Logrus' biggest contribution is to have played a part in today's
15widespread use of structured logging in Golang. There doesn't seem to be a
16reason to do a major, breaking iteration into Logrus V2, since the fantastic Go
17community has built those independently. Many fantastic alternatives have sprung
18up. Logrus would look like those, had it been re-designed with what we know
19about structured logging in Go today. Check out, for example,
20[Zerolog][zerolog], [Zap][zap], and [Apex][apex].
21
22[zerolog]: https://github.com/rs/zerolog
23[zap]: https://github.com/uber-go/zap
24[apex]: https://github.com/apex/log
25
26**Seeing weird case-sensitive problems?** It's in the past been possible to
27import Logrus as both upper- and lower-case. Due to the Go package environment,
28this caused issues in the community and we needed a standard. Some environments
29experienced problems with the upper-case variant, so the lower-case was decided.
30Everything using `logrus` will need to use the lower-case:
31`github.com/sirupsen/logrus`. Any package that isn't, should be changed.
32
33To fix Glide, see [these
34comments](https://github.com/sirupsen/logrus/issues/553#issuecomment-306591437).
35For an in-depth explanation of the casing issue, see [this
36comment](https://github.com/sirupsen/logrus/issues/570#issuecomment-313933276).
37
38Nicely color-coded in development (when a TTY is attached, otherwise just
39plain text):
40
41![Colored](http://i.imgur.com/PY7qMwd.png)
42
43With `log.SetFormatter(&log.JSONFormatter{})`, for easy parsing by logstash
44or Splunk:
45
46```text
47{"animal":"walrus","level":"info","msg":"A group of walrus emerges from the
48ocean","size":10,"time":"2014-03-10 19:57:38.562264131 -0400 EDT"}
49
50{"level":"warning","msg":"The group's number increased tremendously!",
51"number":122,"omg":true,"time":"2014-03-10 19:57:38.562471297 -0400 EDT"}
52
53{"animal":"walrus","level":"info","msg":"A giant walrus appears!",
54"size":10,"time":"2014-03-10 19:57:38.562500591 -0400 EDT"}
55
56{"animal":"walrus","level":"info","msg":"Tremendously sized cow enters the ocean.",
57"size":9,"time":"2014-03-10 19:57:38.562527896 -0400 EDT"}
58
59{"level":"fatal","msg":"The ice breaks!","number":100,"omg":true,
60"time":"2014-03-10 19:57:38.562543128 -0400 EDT"}
61```
62
63With the default `log.SetFormatter(&log.TextFormatter{})` when a TTY is not
64attached, the output is compatible with the
65[logfmt](http://godoc.org/github.com/kr/logfmt) format:
66
67```text
68time="2015-03-26T01:27:38-04:00" level=debug msg="Started observing beach" animal=walrus number=8
69time="2015-03-26T01:27:38-04:00" level=info msg="A group of walrus emerges from the ocean" animal=walrus size=10
70time="2015-03-26T01:27:38-04:00" level=warning msg="The group's number increased tremendously!" number=122 omg=true
71time="2015-03-26T01:27:38-04:00" level=debug msg="Temperature changes" temperature=-4
72time="2015-03-26T01:27:38-04:00" level=panic msg="It's over 9000!" animal=orca size=9009
73time="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
74```
75To ensure this behaviour even if a TTY is attached, set your formatter as follows:
76
77```go
78 log.SetFormatter(&log.TextFormatter{
79 DisableColors: true,
80 FullTimestamp: true,
81 })
82```
83
84#### Logging Method Name
85
86If you wish to add the calling method as a field, instruct the logger via:
87```go
88log.SetReportCaller(true)
89```
90This adds the caller as 'method' like so:
91
92```json
93{"animal":"penguin","level":"fatal","method":"github.com/sirupsen/arcticcreatures.migrate","msg":"a penguin swims by",
94"time":"2014-03-10 19:57:38.562543129 -0400 EDT"}
95```
96
97```text
98time="2015-03-26T01:27:38-04:00" level=fatal method=github.com/sirupsen/arcticcreatures.migrate msg="a penguin swims by" animal=penguin
99```
100Note that this does add measurable overhead - the cost will depend on the version of Go, but is
101between 20 and 40% in recent tests with 1.6 and 1.7. You can validate this in your
102environment via benchmarks:
103```
104go test -bench=.*CallerTracing
105```
106
107
108#### Case-sensitivity
109
110The organization's name was changed to lower-case--and this will not be changed
111back. If you are getting import conflicts due to case sensitivity, please use
112the lower-case import: `github.com/sirupsen/logrus`.
113
114#### Example
115
116The simplest way to use Logrus is simply the package-level exported logger:
117
118```go
119package main
120
121import (
122 log "github.com/sirupsen/logrus"
123)
124
125func main() {
126 log.WithFields(log.Fields{
127 "animal": "walrus",
128 }).Info("A walrus appears")
129}
130```
131
132Note that it's completely api-compatible with the stdlib logger, so you can
133replace your `log` imports everywhere with `log "github.com/sirupsen/logrus"`
134and you'll now have the flexibility of Logrus. You can customize it all you
135want:
136
137```go
138package main
139
140import (
141 "os"
142 log "github.com/sirupsen/logrus"
143)
144
145func init() {
146 // Log as JSON instead of the default ASCII formatter.
147 log.SetFormatter(&log.JSONFormatter{})
148
149 // Output to stdout instead of the default stderr
150 // Can be any io.Writer, see below for File example
151 log.SetOutput(os.Stdout)
152
153 // Only log the warning severity or above.
154 log.SetLevel(log.WarnLevel)
155}
156
157func main() {
158 log.WithFields(log.Fields{
159 "animal": "walrus",
160 "size": 10,
161 }).Info("A group of walrus emerges from the ocean")
162
163 log.WithFields(log.Fields{
164 "omg": true,
165 "number": 122,
166 }).Warn("The group's number increased tremendously!")
167
168 log.WithFields(log.Fields{
169 "omg": true,
170 "number": 100,
171 }).Fatal("The ice breaks!")
172
173 // A common pattern is to re-use fields between logging statements by re-using
174 // the logrus.Entry returned from WithFields()
175 contextLogger := log.WithFields(log.Fields{
176 "common": "this is a common field",
177 "other": "I also should be logged always",
178 })
179
180 contextLogger.Info("I'll be logged with common and other field")
181 contextLogger.Info("Me too")
182}
183```
184
185For more advanced usage such as logging to multiple locations from the same
186application, you can also create an instance of the `logrus` Logger:
187
188```go
189package main
190
191import (
192 "os"
193 "github.com/sirupsen/logrus"
194)
195
196// Create a new instance of the logger. You can have any number of instances.
197var log = logrus.New()
198
199func main() {
200 // The API for setting attributes is a little different than the package level
201 // exported logger. See Godoc.
202 log.Out = os.Stdout
203
204 // You could set this to any `io.Writer` such as a file
205 // file, err := os.OpenFile("logrus.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
206 // if err == nil {
207 // log.Out = file
208 // } else {
209 // log.Info("Failed to log to file, using default stderr")
210 // }
211
212 log.WithFields(logrus.Fields{
213 "animal": "walrus",
214 "size": 10,
215 }).Info("A group of walrus emerges from the ocean")
216}
217```
218
219#### Fields
220
221Logrus encourages careful, structured logging through logging fields instead of
222long, unparseable error messages. For example, instead of: `log.Fatalf("Failed
223to send event %s to topic %s with key %d")`, you should log the much more
224discoverable:
225
226```go
227log.WithFields(log.Fields{
228 "event": event,
229 "topic": topic,
230 "key": key,
231}).Fatal("Failed to send event")
232```
233
234We've found this API forces you to think about logging in a way that produces
235much more useful logging messages. We've been in countless situations where just
236a single added field to a log statement that was already there would've saved us
237hours. The `WithFields` call is optional.
238
239In general, with Logrus using any of the `printf`-family functions should be
240seen as a hint you should add a field, however, you can still use the
241`printf`-family functions with Logrus.
242
243#### Default Fields
244
245Often it's helpful to have fields _always_ attached to log statements in an
246application or parts of one. For example, you may want to always log the
247`request_id` and `user_ip` in the context of a request. Instead of writing
248`log.WithFields(log.Fields{"request_id": request_id, "user_ip": user_ip})` on
249every line, you can create a `logrus.Entry` to pass around instead:
250
251```go
252requestLogger := log.WithFields(log.Fields{"request_id": request_id, "user_ip": user_ip})
253requestLogger.Info("something happened on that request") # will log request_id and user_ip
254requestLogger.Warn("something not great happened")
255```
256
257#### Hooks
258
259You can add hooks for logging levels. For example to send errors to an exception
260tracking service on `Error`, `Fatal` and `Panic`, info to StatsD or log to
261multiple places simultaneously, e.g. syslog.
262
263Logrus comes with [built-in hooks](hooks/). Add those, or your custom hook, in
264`init`:
265
266```go
267import (
268 log "github.com/sirupsen/logrus"
269 "gopkg.in/gemnasium/logrus-airbrake-hook.v2" // the package is named "airbrake"
270 logrus_syslog "github.com/sirupsen/logrus/hooks/syslog"
271 "log/syslog"
272)
273
274func init() {
275
276 // Use the Airbrake hook to report errors that have Error severity or above to
277 // an exception tracker. You can create custom hooks, see the Hooks section.
278 log.AddHook(airbrake.NewHook(123, "xyz", "production"))
279
280 hook, err := logrus_syslog.NewSyslogHook("udp", "localhost:514", syslog.LOG_INFO, "")
281 if err != nil {
282 log.Error("Unable to connect to local syslog daemon")
283 } else {
284 log.AddHook(hook)
285 }
286}
287```
288Note: 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).
289
290A list of currently known service hooks can be found in this wiki [page](https://github.com/sirupsen/logrus/wiki/Hooks)
291
292
293#### Level logging
294
295Logrus has seven logging levels: Trace, Debug, Info, Warning, Error, Fatal and Panic.
296
297```go
298log.Trace("Something very low level.")
299log.Debug("Useful debugging information.")
300log.Info("Something noteworthy happened!")
301log.Warn("You should probably take a look at this.")
302log.Error("Something failed but I'm not quitting.")
303// Calls os.Exit(1) after logging
304log.Fatal("Bye.")
305// Calls panic() after logging
306log.Panic("I'm bailing.")
307```
308
309You can set the logging level on a `Logger`, then it will only log entries with
310that severity or anything above it:
311
312```go
313// Will log anything that is info or above (warn, error, fatal, panic). Default.
314log.SetLevel(log.InfoLevel)
315```
316
317It may be useful to set `log.Level = logrus.DebugLevel` in a debug or verbose
318environment if your application has that.
319
320Note: If you want different log levels for global (`log.SetLevel(...)`) and syslog logging, please check the [syslog hook README](hooks/syslog/README.md#different-log-levels-for-local-and-remote-logging).
321
322#### Entries
323
324Besides the fields added with `WithField` or `WithFields` some fields are
325automatically added to all logging events:
326
3271. `time`. The timestamp when the entry was created.
3282. `msg`. The logging message passed to `{Info,Warn,Error,Fatal,Panic}` after
329 the `AddFields` call. E.g. `Failed to send event.`
3303. `level`. The logging level. E.g. `info`.
331
332#### Environments
333
334Logrus has no notion of environment.
335
336If you wish for hooks and formatters to only be used in specific environments,
337you should handle that yourself. For example, if your application has a global
338variable `Environment`, which is a string representation of the environment you
339could do:
340
341```go
342import (
343 log "github.com/sirupsen/logrus"
344)
345
346func init() {
347 // do something here to set environment depending on an environment variable
348 // or command-line flag
349 if Environment == "production" {
350 log.SetFormatter(&log.JSONFormatter{})
351 } else {
352 // The TextFormatter is default, you don't actually have to do this.
353 log.SetFormatter(&log.TextFormatter{})
354 }
355}
356```
357
358This configuration is how `logrus` was intended to be used, but JSON in
359production is mostly only useful if you do log aggregation with tools like
360Splunk or Logstash.
361
362#### Formatters
363
364The built-in logging formatters are:
365
366* `logrus.TextFormatter`. Logs the event in colors if stdout is a tty, otherwise
367 without colors.
368 * *Note:* to force colored output when there is no TTY, set the `ForceColors`
369 field to `true`. To force no colored output even if there is a TTY set the
370 `DisableColors` field to `true`. For Windows, see
371 [github.com/mattn/go-colorable](https://github.com/mattn/go-colorable).
372 * When colors are enabled, levels are truncated to 4 characters by default. To disable
373 truncation set the `DisableLevelTruncation` field to `true`.
374 * When outputting to a TTY, it's often helpful to visually scan down a column where all the levels are the same width. Setting the `PadLevelText` field to `true` enables this behavior, by adding padding to the level text.
375 * All options are listed in the [generated docs](https://godoc.org/github.com/sirupsen/logrus#TextFormatter).
376* `logrus.JSONFormatter`. Logs fields as JSON.
377 * All options are listed in the [generated docs](https://godoc.org/github.com/sirupsen/logrus#JSONFormatter).
378
379Third party logging formatters:
380
381* [`FluentdFormatter`](https://github.com/joonix/log). Formats entries that can be parsed by Kubernetes and Google Container Engine.
382* [`GELF`](https://github.com/fabienm/go-logrus-formatters). Formats entries so they comply to Graylog's [GELF 1.1 specification](http://docs.graylog.org/en/2.4/pages/gelf.html).
383* [`logstash`](https://github.com/bshuster-repo/logrus-logstash-hook). Logs fields as [Logstash](http://logstash.net) Events.
384* [`prefixed`](https://github.com/x-cray/logrus-prefixed-formatter). Displays log entry source along with alternative layout.
385* [`zalgo`](https://github.com/aybabtme/logzalgo). Invoking the Power of Zalgo.
386* [`nested-logrus-formatter`](https://github.com/antonfisher/nested-logrus-formatter). Converts logrus fields to a nested structure.
387* [`powerful-logrus-formatter`](https://github.com/zput/zxcTool). get fileName, log's line number and the latest function's name when print log; Sava log to files.
388* [`caption-json-formatter`](https://github.com/nolleh/caption_json_formatter). logrus's message json formatter with human-readable caption added.
389
390You can define your formatter by implementing the `Formatter` interface,
391requiring a `Format` method. `Format` takes an `*Entry`. `entry.Data` is a
392`Fields` type (`map[string]interface{}`) with all your fields as well as the
393default ones (see Entries section above):
394
395```go
396type MyJSONFormatter struct {
397}
398
399log.SetFormatter(new(MyJSONFormatter))
400
401func (f *MyJSONFormatter) Format(entry *Entry) ([]byte, error) {
402 // Note this doesn't include Time, Level and Message which are available on
403 // the Entry. Consult `godoc` on information about those fields or read the
404 // source of the official loggers.
405 serialized, err := json.Marshal(entry.Data)
406 if err != nil {
407 return nil, fmt.Errorf("Failed to marshal fields to JSON, %w", err)
408 }
409 return append(serialized, '\n'), nil
410}
411```
412
413#### Logger as an `io.Writer`
414
415Logrus can be transformed into an `io.Writer`. That writer is the end of an `io.Pipe` and it is your responsibility to close it.
416
417```go
418w := logger.Writer()
419defer w.Close()
420
421srv := http.Server{
422 // create a stdlib log.Logger that writes to
423 // logrus.Logger.
424 ErrorLog: log.New(w, "", 0),
425}
426```
427
428Each line written to that writer will be printed the usual way, using formatters
429and hooks. The level for those entries is `info`.
430
431This means that we can override the standard library logger easily:
432
433```go
434logger := logrus.New()
435logger.Formatter = &logrus.JSONFormatter{}
436
437// Use logrus for standard log output
438// Note that `log` here references stdlib's log
439// Not logrus imported under the name `log`.
440log.SetOutput(logger.Writer())
441```
442
443#### Rotation
444
445Log rotation is not provided with Logrus. Log rotation should be done by an
446external program (like `logrotate(8)`) that can compress and delete old log
447entries. It should not be a feature of the application-level logger.
448
449#### Tools
450
451| Tool | Description |
452| ---- | ----------- |
453|[Logrus Mate](https://github.com/gogap/logrus_mate)|Logrus mate is a tool for Logrus to manage loggers, you can initial logger's level, hook and formatter by config file, the logger will be generated with different configs in different environments.|
454|[Logrus Viper Helper](https://github.com/heirko/go-contrib/tree/master/logrusHelper)|An Helper around Logrus to wrap with spf13/Viper to load configuration with fangs! And to simplify Logrus configuration use some behavior of [Logrus Mate](https://github.com/gogap/logrus_mate). [sample](https://github.com/heirko/iris-contrib/blob/master/middleware/logrus-logger/example) |
455
456#### Testing
457
458Logrus has a built in facility for asserting the presence of log messages. This is implemented through the `test` hook and provides:
459
460* decorators for existing logger (`test.NewLocal` and `test.NewGlobal`) which basically just adds the `test` hook
461* a test logger (`test.NewNullLogger`) that just records log messages (and does not output any):
462
463```go
464import(
465 "github.com/sirupsen/logrus"
466 "github.com/sirupsen/logrus/hooks/test"
467 "github.com/stretchr/testify/assert"
468 "testing"
469)
470
471func TestSomething(t*testing.T){
472 logger, hook := test.NewNullLogger()
473 logger.Error("Helloerror")
474
475 assert.Equal(t, 1, len(hook.Entries))
476 assert.Equal(t, logrus.ErrorLevel, hook.LastEntry().Level)
477 assert.Equal(t, "Helloerror", hook.LastEntry().Message)
478
479 hook.Reset()
480 assert.Nil(t, hook.LastEntry())
481}
482```
483
484#### Fatal handlers
485
486Logrus can register one or more functions that will be called when any `fatal`
487level message is logged. The registered handlers will be executed before
488logrus performs an `os.Exit(1)`. This behavior may be helpful if callers need
489to gracefully shutdown. Unlike a `panic("Something went wrong...")` call which can be intercepted with a deferred `recover` a call to `os.Exit(1)` can not be intercepted.
490
491```
492...
493handler := func() {
494 // gracefully shutdown something...
495}
496logrus.RegisterExitHandler(handler)
497...
498```
499
500#### Thread safety
501
502By default, Logger is protected by a mutex for concurrent writes. The mutex is held when calling hooks and writing logs.
503If you are sure such locking is not needed, you can call logger.SetNoLock() to disable the locking.
504
505Situation when locking is not needed includes:
506
507* You have no hooks registered, or hooks calling is already thread-safe.
508
509* Writing to logger.Out is already thread-safe, for example:
510
511 1) logger.Out is protected by locks.
512
513 2) logger.Out is an os.File handler opened with `O_APPEND` flag, and every write is smaller than 4k. (This allows multi-thread/multi-process writing)
514
515 (Refer to http://www.notthewizard.com/2014/06/17/are-files-appends-really-atomic/)
diff --git a/vendor/github.com/sirupsen/logrus/alt_exit.go b/vendor/github.com/sirupsen/logrus/alt_exit.go
new file mode 100644
index 0000000..8fd189e
--- /dev/null
+++ b/vendor/github.com/sirupsen/logrus/alt_exit.go
@@ -0,0 +1,76 @@
1package logrus
2
3// The following code was sourced and modified from the
4// https://github.com/tebeka/atexit package governed by the following license:
5//
6// Copyright (c) 2012 Miki Tebeka <[email protected]>.
7//
8// Permission is hereby granted, free of charge, to any person obtaining a copy of
9// this software and associated documentation files (the "Software"), to deal in
10// the Software without restriction, including without limitation the rights to
11// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
12// the Software, and to permit persons to whom the Software is furnished to do so,
13// subject to the following conditions:
14//
15// The above copyright notice and this permission notice shall be included in all
16// copies or substantial portions of the Software.
17//
18// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
20// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
21// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
22// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
25import (
26 "fmt"
27 "os"
28)
29
30var handlers = []func(){}
31
32func runHandler(handler func()) {
33 defer func() {
34 if err := recover(); err != nil {
35 fmt.Fprintln(os.Stderr, "Error: Logrus exit handler error:", err)
36 }
37 }()
38
39 handler()
40}
41
42func runHandlers() {
43 for _, handler := range handlers {
44 runHandler(handler)
45 }
46}
47
48// Exit runs all the Logrus atexit handlers and then terminates the program using os.Exit(code)
49func Exit(code int) {
50 runHandlers()
51 os.Exit(code)
52}
53
54// RegisterExitHandler appends a Logrus Exit handler to the list of handlers,
55// call logrus.Exit to invoke all handlers. The handlers will also be invoked when
56// any Fatal log entry is made.
57//
58// This method is useful when a caller wishes to use logrus to log a fatal
59// message but also needs to gracefully shutdown. An example usecase could be
60// closing database connections, or sending a alert that the application is
61// closing.
62func RegisterExitHandler(handler func()) {
63 handlers = append(handlers, handler)
64}
65
66// DeferExitHandler prepends a Logrus Exit handler to the list of handlers,
67// call logrus.Exit to invoke all handlers. The handlers will also be invoked when
68// any Fatal log entry is made.
69//
70// This method is useful when a caller wishes to use logrus to log a fatal
71// message but also needs to gracefully shutdown. An example usecase could be
72// closing database connections, or sending a alert that the application is
73// closing.
74func DeferExitHandler(handler func()) {
75 handlers = append([]func(){handler}, handlers...)
76}
diff --git a/vendor/github.com/sirupsen/logrus/appveyor.yml b/vendor/github.com/sirupsen/logrus/appveyor.yml
new file mode 100644
index 0000000..df9d65c
--- /dev/null
+++ b/vendor/github.com/sirupsen/logrus/appveyor.yml
@@ -0,0 +1,14 @@
1version: "{build}"
2platform: x64
3clone_folder: c:\gopath\src\github.com\sirupsen\logrus
4environment:
5 GOPATH: c:\gopath
6branches:
7 only:
8 - master
9install:
10 - set PATH=%GOPATH%\bin;c:\go\bin;%PATH%
11 - go version
12build_script:
13 - go get -t
14 - go test
diff --git a/vendor/github.com/sirupsen/logrus/buffer_pool.go b/vendor/github.com/sirupsen/logrus/buffer_pool.go
new file mode 100644
index 0000000..c7787f7
--- /dev/null
+++ b/vendor/github.com/sirupsen/logrus/buffer_pool.go
@@ -0,0 +1,43 @@
1package logrus
2
3import (
4 "bytes"
5 "sync"
6)
7
8var (
9 bufferPool BufferPool
10)
11
12type BufferPool interface {
13 Put(*bytes.Buffer)
14 Get() *bytes.Buffer
15}
16
17type defaultPool struct {
18 pool *sync.Pool
19}
20
21func (p *defaultPool) Put(buf *bytes.Buffer) {
22 p.pool.Put(buf)
23}
24
25func (p *defaultPool) Get() *bytes.Buffer {
26 return p.pool.Get().(*bytes.Buffer)
27}
28
29// SetBufferPool allows to replace the default logrus buffer pool
30// to better meets the specific needs of an application.
31func SetBufferPool(bp BufferPool) {
32 bufferPool = bp
33}
34
35func init() {
36 SetBufferPool(&defaultPool{
37 pool: &sync.Pool{
38 New: func() interface{} {
39 return new(bytes.Buffer)
40 },
41 },
42 })
43}
diff --git a/vendor/github.com/sirupsen/logrus/doc.go b/vendor/github.com/sirupsen/logrus/doc.go
new file mode 100644
index 0000000..da67aba
--- /dev/null
+++ b/vendor/github.com/sirupsen/logrus/doc.go
@@ -0,0 +1,26 @@
1/*
2Package logrus is a structured logger for Go, completely API compatible with the standard library logger.
3
4
5The simplest way to use Logrus is simply the package-level exported logger:
6
7 package main
8
9 import (
10 log "github.com/sirupsen/logrus"
11 )
12
13 func main() {
14 log.WithFields(log.Fields{
15 "animal": "walrus",
16 "number": 1,
17 "size": 10,
18 }).Info("A walrus appears")
19 }
20
21Output:
22 time="2015-09-07T08:48:33Z" level=info msg="A walrus appears" animal=walrus number=1 size=10
23
24For a full guide visit https://github.com/sirupsen/logrus
25*/
26package logrus
diff --git a/vendor/github.com/sirupsen/logrus/entry.go b/vendor/github.com/sirupsen/logrus/entry.go
new file mode 100644
index 0000000..71cdbbc
--- /dev/null
+++ b/vendor/github.com/sirupsen/logrus/entry.go
@@ -0,0 +1,442 @@
1package logrus
2
3import (
4 "bytes"
5 "context"
6 "fmt"
7 "os"
8 "reflect"
9 "runtime"
10 "strings"
11 "sync"
12 "time"
13)
14
15var (
16
17 // qualified package name, cached at first use
18 logrusPackage string
19
20 // Positions in the call stack when tracing to report the calling method
21 minimumCallerDepth int
22
23 // Used for caller information initialisation
24 callerInitOnce sync.Once
25)
26
27const (
28 maximumCallerDepth int = 25
29 knownLogrusFrames int = 4
30)
31
32func init() {
33 // start at the bottom of the stack before the package-name cache is primed
34 minimumCallerDepth = 1
35}
36
37// Defines the key when adding errors using WithError.
38var ErrorKey = "error"
39
40// An entry is the final or intermediate Logrus logging entry. It contains all
41// the fields passed with WithField{,s}. It's finally logged when Trace, Debug,
42// Info, Warn, Error, Fatal or Panic is called on it. These objects can be
43// reused and passed around as much as you wish to avoid field duplication.
44type Entry struct {
45 Logger *Logger
46
47 // Contains all the fields set by the user.
48 Data Fields
49
50 // Time at which the log entry was created
51 Time time.Time
52
53 // Level the log entry was logged at: Trace, Debug, Info, Warn, Error, Fatal or Panic
54 // This field will be set on entry firing and the value will be equal to the one in Logger struct field.
55 Level Level
56
57 // Calling method, with package name
58 Caller *runtime.Frame
59
60 // Message passed to Trace, Debug, Info, Warn, Error, Fatal or Panic
61 Message string
62
63 // When formatter is called in entry.log(), a Buffer may be set to entry
64 Buffer *bytes.Buffer
65
66 // Contains the context set by the user. Useful for hook processing etc.
67 Context context.Context
68
69 // err may contain a field formatting error
70 err string
71}
72
73func NewEntry(logger *Logger) *Entry {
74 return &Entry{
75 Logger: logger,
76 // Default is three fields, plus one optional. Give a little extra room.
77 Data: make(Fields, 6),
78 }
79}
80
81func (entry *Entry) Dup() *Entry {
82 data := make(Fields, len(entry.Data))
83 for k, v := range entry.Data {
84 data[k] = v
85 }
86 return &Entry{Logger: entry.Logger, Data: data, Time: entry.Time, Context: entry.Context, err: entry.err}
87}
88
89// Returns the bytes representation of this entry from the formatter.
90func (entry *Entry) Bytes() ([]byte, error) {
91 return entry.Logger.Formatter.Format(entry)
92}
93
94// Returns the string representation from the reader and ultimately the
95// formatter.
96func (entry *Entry) String() (string, error) {
97 serialized, err := entry.Bytes()
98 if err != nil {
99 return "", err
100 }
101 str := string(serialized)
102 return str, nil
103}
104
105// Add an error as single field (using the key defined in ErrorKey) to the Entry.
106func (entry *Entry) WithError(err error) *Entry {
107 return entry.WithField(ErrorKey, err)
108}
109
110// Add a context to the Entry.
111func (entry *Entry) WithContext(ctx context.Context) *Entry {
112 dataCopy := make(Fields, len(entry.Data))
113 for k, v := range entry.Data {
114 dataCopy[k] = v
115 }
116 return &Entry{Logger: entry.Logger, Data: dataCopy, Time: entry.Time, err: entry.err, Context: ctx}
117}
118
119// Add a single field to the Entry.
120func (entry *Entry) WithField(key string, value interface{}) *Entry {
121 return entry.WithFields(Fields{key: value})
122}
123
124// Add a map of fields to the Entry.
125func (entry *Entry) WithFields(fields Fields) *Entry {
126 data := make(Fields, len(entry.Data)+len(fields))
127 for k, v := range entry.Data {
128 data[k] = v
129 }
130 fieldErr := entry.err
131 for k, v := range fields {
132 isErrField := false
133 if t := reflect.TypeOf(v); t != nil {
134 switch {
135 case t.Kind() == reflect.Func, t.Kind() == reflect.Ptr && t.Elem().Kind() == reflect.Func:
136 isErrField = true
137 }
138 }
139 if isErrField {
140 tmp := fmt.Sprintf("can not add field %q", k)
141 if fieldErr != "" {
142 fieldErr = entry.err + ", " + tmp
143 } else {
144 fieldErr = tmp
145 }
146 } else {
147 data[k] = v
148 }
149 }
150 return &Entry{Logger: entry.Logger, Data: data, Time: entry.Time, err: fieldErr, Context: entry.Context}
151}
152
153// Overrides the time of the Entry.
154func (entry *Entry) WithTime(t time.Time) *Entry {
155 dataCopy := make(Fields, len(entry.Data))
156 for k, v := range entry.Data {
157 dataCopy[k] = v
158 }
159 return &Entry{Logger: entry.Logger, Data: dataCopy, Time: t, err: entry.err, Context: entry.Context}
160}
161
162// getPackageName reduces a fully qualified function name to the package name
163// There really ought to be to be a better way...
164func getPackageName(f string) string {
165 for {
166 lastPeriod := strings.LastIndex(f, ".")
167 lastSlash := strings.LastIndex(f, "/")
168 if lastPeriod > lastSlash {
169 f = f[:lastPeriod]
170 } else {
171 break
172 }
173 }
174
175 return f
176}
177
178// getCaller retrieves the name of the first non-logrus calling function
179func getCaller() *runtime.Frame {
180 // cache this package's fully-qualified name
181 callerInitOnce.Do(func() {
182 pcs := make([]uintptr, maximumCallerDepth)
183 _ = runtime.Callers(0, pcs)
184
185 // dynamic get the package name and the minimum caller depth
186 for i := 0; i < maximumCallerDepth; i++ {
187 funcName := runtime.FuncForPC(pcs[i]).Name()
188 if strings.Contains(funcName, "getCaller") {
189 logrusPackage = getPackageName(funcName)
190 break
191 }
192 }
193
194 minimumCallerDepth = knownLogrusFrames
195 })
196
197 // Restrict the lookback frames to avoid runaway lookups
198 pcs := make([]uintptr, maximumCallerDepth)
199 depth := runtime.Callers(minimumCallerDepth, pcs)
200 frames := runtime.CallersFrames(pcs[:depth])
201
202 for f, again := frames.Next(); again; f, again = frames.Next() {
203 pkg := getPackageName(f.Function)
204
205 // If the caller isn't part of this package, we're done
206 if pkg != logrusPackage {
207 return &f //nolint:scopelint
208 }
209 }
210
211 // if we got here, we failed to find the caller's context
212 return nil
213}
214
215func (entry Entry) HasCaller() (has bool) {
216 return entry.Logger != nil &&
217 entry.Logger.ReportCaller &&
218 entry.Caller != nil
219}
220
221func (entry *Entry) log(level Level, msg string) {
222 var buffer *bytes.Buffer
223
224 newEntry := entry.Dup()
225
226 if newEntry.Time.IsZero() {
227 newEntry.Time = time.Now()
228 }
229
230 newEntry.Level = level
231 newEntry.Message = msg
232
233 newEntry.Logger.mu.Lock()
234 reportCaller := newEntry.Logger.ReportCaller
235 bufPool := newEntry.getBufferPool()
236 newEntry.Logger.mu.Unlock()
237
238 if reportCaller {
239 newEntry.Caller = getCaller()
240 }
241
242 newEntry.fireHooks()
243 buffer = bufPool.Get()
244 defer func() {
245 newEntry.Buffer = nil
246 buffer.Reset()
247 bufPool.Put(buffer)
248 }()
249 buffer.Reset()
250 newEntry.Buffer = buffer
251
252 newEntry.write()
253
254 newEntry.Buffer = nil
255
256 // To avoid Entry#log() returning a value that only would make sense for
257 // panic() to use in Entry#Panic(), we avoid the allocation by checking
258 // directly here.
259 if level <= PanicLevel {
260 panic(newEntry)
261 }
262}
263
264func (entry *Entry) getBufferPool() (pool BufferPool) {
265 if entry.Logger.BufferPool != nil {
266 return entry.Logger.BufferPool
267 }
268 return bufferPool
269}
270
271func (entry *Entry) fireHooks() {
272 var tmpHooks LevelHooks
273 entry.Logger.mu.Lock()
274 tmpHooks = make(LevelHooks, len(entry.Logger.Hooks))
275 for k, v := range entry.Logger.Hooks {
276 tmpHooks[k] = v
277 }
278 entry.Logger.mu.Unlock()
279
280 err := tmpHooks.Fire(entry.Level, entry)
281 if err != nil {
282 fmt.Fprintf(os.Stderr, "Failed to fire hook: %v\n", err)
283 }
284}
285
286func (entry *Entry) write() {
287 entry.Logger.mu.Lock()
288 defer entry.Logger.mu.Unlock()
289 serialized, err := entry.Logger.Formatter.Format(entry)
290 if err != nil {
291 fmt.Fprintf(os.Stderr, "Failed to obtain reader, %v\n", err)
292 return
293 }
294 if _, err := entry.Logger.Out.Write(serialized); err != nil {
295 fmt.Fprintf(os.Stderr, "Failed to write to log, %v\n", err)
296 }
297}
298
299// Log will log a message at the level given as parameter.
300// Warning: using Log at Panic or Fatal level will not respectively Panic nor Exit.
301// For this behaviour Entry.Panic or Entry.Fatal should be used instead.
302func (entry *Entry) Log(level Level, args ...interface{}) {
303 if entry.Logger.IsLevelEnabled(level) {
304 entry.log(level, fmt.Sprint(args...))
305 }
306}
307
308func (entry *Entry) Trace(args ...interface{}) {
309 entry.Log(TraceLevel, args...)
310}
311
312func (entry *Entry) Debug(args ...interface{}) {
313 entry.Log(DebugLevel, args...)
314}
315
316func (entry *Entry) Print(args ...interface{}) {
317 entry.Info(args...)
318}
319
320func (entry *Entry) Info(args ...interface{}) {
321 entry.Log(InfoLevel, args...)
322}
323
324func (entry *Entry) Warn(args ...interface{}) {
325 entry.Log(WarnLevel, args...)
326}
327
328func (entry *Entry) Warning(args ...interface{}) {
329 entry.Warn(args...)
330}
331
332func (entry *Entry) Error(args ...interface{}) {
333 entry.Log(ErrorLevel, args...)
334}
335
336func (entry *Entry) Fatal(args ...interface{}) {
337 entry.Log(FatalLevel, args...)
338 entry.Logger.Exit(1)
339}
340
341func (entry *Entry) Panic(args ...interface{}) {
342 entry.Log(PanicLevel, args...)
343}
344
345// Entry Printf family functions
346
347func (entry *Entry) Logf(level Level, format string, args ...interface{}) {
348 if entry.Logger.IsLevelEnabled(level) {
349 entry.Log(level, fmt.Sprintf(format, args...))
350 }
351}
352
353func (entry *Entry) Tracef(format string, args ...interface{}) {
354 entry.Logf(TraceLevel, format, args...)
355}
356
357func (entry *Entry) Debugf(format string, args ...interface{}) {
358 entry.Logf(DebugLevel, format, args...)
359}
360
361func (entry *Entry) Infof(format string, args ...interface{}) {
362 entry.Logf(InfoLevel, format, args...)
363}
364
365func (entry *Entry) Printf(format string, args ...interface{}) {
366 entry.Infof(format, args...)
367}
368
369func (entry *Entry) Warnf(format string, args ...interface{}) {
370 entry.Logf(WarnLevel, format, args...)
371}
372
373func (entry *Entry) Warningf(format string, args ...interface{}) {
374 entry.Warnf(format, args...)
375}
376
377func (entry *Entry) Errorf(format string, args ...interface{}) {
378 entry.Logf(ErrorLevel, format, args...)
379}
380
381func (entry *Entry) Fatalf(format string, args ...interface{}) {
382 entry.Logf(FatalLevel, format, args...)
383 entry.Logger.Exit(1)
384}
385
386func (entry *Entry) Panicf(format string, args ...interface{}) {
387 entry.Logf(PanicLevel, format, args...)
388}
389
390// Entry Println family functions
391
392func (entry *Entry) Logln(level Level, args ...interface{}) {
393 if entry.Logger.IsLevelEnabled(level) {
394 entry.Log(level, entry.sprintlnn(args...))
395 }
396}
397
398func (entry *Entry) Traceln(args ...interface{}) {
399 entry.Logln(TraceLevel, args...)
400}
401
402func (entry *Entry) Debugln(args ...interface{}) {
403 entry.Logln(DebugLevel, args...)
404}
405
406func (entry *Entry) Infoln(args ...interface{}) {
407 entry.Logln(InfoLevel, args...)
408}
409
410func (entry *Entry) Println(args ...interface{}) {
411 entry.Infoln(args...)
412}
413
414func (entry *Entry) Warnln(args ...interface{}) {
415 entry.Logln(WarnLevel, args...)
416}
417
418func (entry *Entry) Warningln(args ...interface{}) {
419 entry.Warnln(args...)
420}
421
422func (entry *Entry) Errorln(args ...interface{}) {
423 entry.Logln(ErrorLevel, args...)
424}
425
426func (entry *Entry) Fatalln(args ...interface{}) {
427 entry.Logln(FatalLevel, args...)
428 entry.Logger.Exit(1)
429}
430
431func (entry *Entry) Panicln(args ...interface{}) {
432 entry.Logln(PanicLevel, args...)
433}
434
435// Sprintlnn => Sprint no newline. This is to get the behavior of how
436// fmt.Sprintln where spaces are always added between operands, regardless of
437// their type. Instead of vendoring the Sprintln implementation to spare a
438// string allocation, we do the simplest thing.
439func (entry *Entry) sprintlnn(args ...interface{}) string {
440 msg := fmt.Sprintln(args...)
441 return msg[:len(msg)-1]
442}
diff --git a/vendor/github.com/sirupsen/logrus/exported.go b/vendor/github.com/sirupsen/logrus/exported.go
new file mode 100644
index 0000000..017c30c
--- /dev/null
+++ b/vendor/github.com/sirupsen/logrus/exported.go
@@ -0,0 +1,270 @@
1package logrus
2
3import (
4 "context"
5 "io"
6 "time"
7)
8
9var (
10 // std is the name of the standard logger in stdlib `log`
11 std = New()
12)
13
14func StandardLogger() *Logger {
15 return std
16}
17
18// SetOutput sets the standard logger output.
19func SetOutput(out io.Writer) {
20 std.SetOutput(out)
21}
22
23// SetFormatter sets the standard logger formatter.
24func SetFormatter(formatter Formatter) {
25 std.SetFormatter(formatter)
26}
27
28// SetReportCaller sets whether the standard logger will include the calling
29// method as a field.
30func SetReportCaller(include bool) {
31 std.SetReportCaller(include)
32}
33
34// SetLevel sets the standard logger level.
35func SetLevel(level Level) {
36 std.SetLevel(level)
37}
38
39// GetLevel returns the standard logger level.
40func GetLevel() Level {
41 return std.GetLevel()
42}
43
44// IsLevelEnabled checks if the log level of the standard logger is greater than the level param
45func IsLevelEnabled(level Level) bool {
46 return std.IsLevelEnabled(level)
47}
48
49// AddHook adds a hook to the standard logger hooks.
50func AddHook(hook Hook) {
51 std.AddHook(hook)
52}
53
54// WithError creates an entry from the standard logger and adds an error to it, using the value defined in ErrorKey as key.
55func WithError(err error) *Entry {
56 return std.WithField(ErrorKey, err)
57}
58
59// WithContext creates an entry from the standard logger and adds a context to it.
60func WithContext(ctx context.Context) *Entry {
61 return std.WithContext(ctx)
62}
63
64// WithField creates an entry from the standard logger and adds a field to
65// it. If you want multiple fields, use `WithFields`.
66//
67// Note that it doesn't log until you call Debug, Print, Info, Warn, Fatal
68// or Panic on the Entry it returns.
69func WithField(key string, value interface{}) *Entry {
70 return std.WithField(key, value)
71}
72
73// WithFields creates an entry from the standard logger and adds multiple
74// fields to it. This is simply a helper for `WithField`, invoking it
75// once for each field.
76//
77// Note that it doesn't log until you call Debug, Print, Info, Warn, Fatal
78// or Panic on the Entry it returns.
79func WithFields(fields Fields) *Entry {
80 return std.WithFields(fields)
81}
82
83// WithTime creates an entry from the standard logger and overrides the time of
84// logs generated with it.
85//
86// Note that it doesn't log until you call Debug, Print, Info, Warn, Fatal
87// or Panic on the Entry it returns.
88func WithTime(t time.Time) *Entry {
89 return std.WithTime(t)
90}
91
92// Trace logs a message at level Trace on the standard logger.
93func Trace(args ...interface{}) {
94 std.Trace(args...)
95}
96
97// Debug logs a message at level Debug on the standard logger.
98func Debug(args ...interface{}) {
99 std.Debug(args...)
100}
101
102// Print logs a message at level Info on the standard logger.
103func Print(args ...interface{}) {
104 std.Print(args...)
105}
106
107// Info logs a message at level Info on the standard logger.
108func Info(args ...interface{}) {
109 std.Info(args...)
110}
111
112// Warn logs a message at level Warn on the standard logger.
113func Warn(args ...interface{}) {
114 std.Warn(args...)
115}
116
117// Warning logs a message at level Warn on the standard logger.
118func Warning(args ...interface{}) {
119 std.Warning(args...)
120}
121
122// Error logs a message at level Error on the standard logger.
123func Error(args ...interface{}) {
124 std.Error(args...)
125}
126
127// Panic logs a message at level Panic on the standard logger.
128func Panic(args ...interface{}) {
129 std.Panic(args...)
130}
131
132// Fatal logs a message at level Fatal on the standard logger then the process will exit with status set to 1.
133func Fatal(args ...interface{}) {
134 std.Fatal(args...)
135}
136
137// TraceFn logs a message from a func at level Trace on the standard logger.
138func TraceFn(fn LogFunction) {
139 std.TraceFn(fn)
140}
141
142// DebugFn logs a message from a func at level Debug on the standard logger.
143func DebugFn(fn LogFunction) {
144 std.DebugFn(fn)
145}
146
147// PrintFn logs a message from a func at level Info on the standard logger.
148func PrintFn(fn LogFunction) {
149 std.PrintFn(fn)
150}
151
152// InfoFn logs a message from a func at level Info on the standard logger.
153func InfoFn(fn LogFunction) {
154 std.InfoFn(fn)
155}
156
157// WarnFn logs a message from a func at level Warn on the standard logger.
158func WarnFn(fn LogFunction) {
159 std.WarnFn(fn)
160}
161
162// WarningFn logs a message from a func at level Warn on the standard logger.
163func WarningFn(fn LogFunction) {
164 std.WarningFn(fn)
165}
166
167// ErrorFn logs a message from a func at level Error on the standard logger.
168func ErrorFn(fn LogFunction) {
169 std.ErrorFn(fn)
170}
171
172// PanicFn logs a message from a func at level Panic on the standard logger.
173func PanicFn(fn LogFunction) {
174 std.PanicFn(fn)
175}
176
177// FatalFn logs a message from a func at level Fatal on the standard logger then the process will exit with status set to 1.
178func FatalFn(fn LogFunction) {
179 std.FatalFn(fn)
180}
181
182// Tracef logs a message at level Trace on the standard logger.
183func Tracef(format string, args ...interface{}) {
184 std.Tracef(format, args...)
185}
186
187// Debugf logs a message at level Debug on the standard logger.
188func Debugf(format string, args ...interface{}) {
189 std.Debugf(format, args...)
190}
191
192// Printf logs a message at level Info on the standard logger.
193func Printf(format string, args ...interface{}) {
194 std.Printf(format, args...)
195}
196
197// Infof logs a message at level Info on the standard logger.
198func Infof(format string, args ...interface{}) {
199 std.Infof(format, args...)
200}
201
202// Warnf logs a message at level Warn on the standard logger.
203func Warnf(format string, args ...interface{}) {
204 std.Warnf(format, args...)
205}
206
207// Warningf logs a message at level Warn on the standard logger.
208func Warningf(format string, args ...interface{}) {
209 std.Warningf(format, args...)
210}
211
212// Errorf logs a message at level Error on the standard logger.
213func Errorf(format string, args ...interface{}) {
214 std.Errorf(format, args...)
215}
216
217// Panicf logs a message at level Panic on the standard logger.
218func Panicf(format string, args ...interface{}) {
219 std.Panicf(format, args...)
220}
221
222// Fatalf logs a message at level Fatal on the standard logger then the process will exit with status set to 1.
223func Fatalf(format string, args ...interface{}) {
224 std.Fatalf(format, args...)
225}
226
227// Traceln logs a message at level Trace on the standard logger.
228func Traceln(args ...interface{}) {
229 std.Traceln(args...)
230}
231
232// Debugln logs a message at level Debug on the standard logger.
233func Debugln(args ...interface{}) {
234 std.Debugln(args...)
235}
236
237// Println logs a message at level Info on the standard logger.
238func Println(args ...interface{}) {
239 std.Println(args...)
240}
241
242// Infoln logs a message at level Info on the standard logger.
243func Infoln(args ...interface{}) {
244 std.Infoln(args...)
245}
246
247// Warnln logs a message at level Warn on the standard logger.
248func Warnln(args ...interface{}) {
249 std.Warnln(args...)
250}
251
252// Warningln logs a message at level Warn on the standard logger.
253func Warningln(args ...interface{}) {
254 std.Warningln(args...)
255}
256
257// Errorln logs a message at level Error on the standard logger.
258func Errorln(args ...interface{}) {
259 std.Errorln(args...)
260}
261
262// Panicln logs a message at level Panic on the standard logger.
263func Panicln(args ...interface{}) {
264 std.Panicln(args...)
265}
266
267// Fatalln logs a message at level Fatal on the standard logger then the process will exit with status set to 1.
268func Fatalln(args ...interface{}) {
269 std.Fatalln(args...)
270}
diff --git a/vendor/github.com/sirupsen/logrus/formatter.go b/vendor/github.com/sirupsen/logrus/formatter.go
new file mode 100644
index 0000000..4088837
--- /dev/null
+++ b/vendor/github.com/sirupsen/logrus/formatter.go
@@ -0,0 +1,78 @@
1package logrus
2
3import "time"
4
5// Default key names for the default fields
6const (
7 defaultTimestampFormat = time.RFC3339
8 FieldKeyMsg = "msg"
9 FieldKeyLevel = "level"
10 FieldKeyTime = "time"
11 FieldKeyLogrusError = "logrus_error"
12 FieldKeyFunc = "func"
13 FieldKeyFile = "file"
14)
15
16// The Formatter interface is used to implement a custom Formatter. It takes an
17// `Entry`. It exposes all the fields, including the default ones:
18//
19// * `entry.Data["msg"]`. The message passed from Info, Warn, Error ..
20// * `entry.Data["time"]`. The timestamp.
21// * `entry.Data["level"]. The level the entry was logged at.
22//
23// Any additional fields added with `WithField` or `WithFields` are also in
24// `entry.Data`. Format is expected to return an array of bytes which are then
25// logged to `logger.Out`.
26type Formatter interface {
27 Format(*Entry) ([]byte, error)
28}
29
30// This is to not silently overwrite `time`, `msg`, `func` and `level` fields when
31// dumping it. If this code wasn't there doing:
32//
33// logrus.WithField("level", 1).Info("hello")
34//
35// Would just silently drop the user provided level. Instead with this code
36// it'll logged as:
37//
38// {"level": "info", "fields.level": 1, "msg": "hello", "time": "..."}
39//
40// It's not exported because it's still using Data in an opinionated way. It's to
41// avoid code duplication between the two default formatters.
42func prefixFieldClashes(data Fields, fieldMap FieldMap, reportCaller bool) {
43 timeKey := fieldMap.resolve(FieldKeyTime)
44 if t, ok := data[timeKey]; ok {
45 data["fields."+timeKey] = t
46 delete(data, timeKey)
47 }
48
49 msgKey := fieldMap.resolve(FieldKeyMsg)
50 if m, ok := data[msgKey]; ok {
51 data["fields."+msgKey] = m
52 delete(data, msgKey)
53 }
54
55 levelKey := fieldMap.resolve(FieldKeyLevel)
56 if l, ok := data[levelKey]; ok {
57 data["fields."+levelKey] = l
58 delete(data, levelKey)
59 }
60
61 logrusErrKey := fieldMap.resolve(FieldKeyLogrusError)
62 if l, ok := data[logrusErrKey]; ok {
63 data["fields."+logrusErrKey] = l
64 delete(data, logrusErrKey)
65 }
66
67 // If reportCaller is not set, 'func' will not conflict.
68 if reportCaller {
69 funcKey := fieldMap.resolve(FieldKeyFunc)
70 if l, ok := data[funcKey]; ok {
71 data["fields."+funcKey] = l
72 }
73 fileKey := fieldMap.resolve(FieldKeyFile)
74 if l, ok := data[fileKey]; ok {
75 data["fields."+fileKey] = l
76 }
77 }
78}
diff --git a/vendor/github.com/sirupsen/logrus/hooks.go b/vendor/github.com/sirupsen/logrus/hooks.go
new file mode 100644
index 0000000..3f151cd
--- /dev/null
+++ b/vendor/github.com/sirupsen/logrus/hooks.go
@@ -0,0 +1,34 @@
1package logrus
2
3// A hook to be fired when logging on the logging levels returned from
4// `Levels()` on your implementation of the interface. Note that this is not
5// fired in a goroutine or a channel with workers, you should handle such
6// functionality yourself if your call is non-blocking and you don't wish for
7// the logging calls for levels returned from `Levels()` to block.
8type Hook interface {
9 Levels() []Level
10 Fire(*Entry) error
11}
12
13// Internal type for storing the hooks on a logger instance.
14type LevelHooks map[Level][]Hook
15
16// Add a hook to an instance of logger. This is called with
17// `log.Hooks.Add(new(MyHook))` where `MyHook` implements the `Hook` interface.
18func (hooks LevelHooks) Add(hook Hook) {
19 for _, level := range hook.Levels() {
20 hooks[level] = append(hooks[level], hook)
21 }
22}
23
24// Fire all the hooks for the passed level. Used by `entry.log` to fire
25// appropriate hooks for a log entry.
26func (hooks LevelHooks) Fire(level Level, entry *Entry) error {
27 for _, hook := range hooks[level] {
28 if err := hook.Fire(entry); err != nil {
29 return err
30 }
31 }
32
33 return nil
34}
diff --git a/vendor/github.com/sirupsen/logrus/json_formatter.go b/vendor/github.com/sirupsen/logrus/json_formatter.go
new file mode 100644
index 0000000..c96dc56
--- /dev/null
+++ b/vendor/github.com/sirupsen/logrus/json_formatter.go
@@ -0,0 +1,128 @@
1package logrus
2
3import (
4 "bytes"
5 "encoding/json"
6 "fmt"
7 "runtime"
8)
9
10type fieldKey string
11
12// FieldMap allows customization of the key names for default fields.
13type FieldMap map[fieldKey]string
14
15func (f FieldMap) resolve(key fieldKey) string {
16 if k, ok := f[key]; ok {
17 return k
18 }
19
20 return string(key)
21}
22
23// JSONFormatter formats logs into parsable json
24type JSONFormatter struct {
25 // TimestampFormat sets the format used for marshaling timestamps.
26 // The format to use is the same than for time.Format or time.Parse from the standard
27 // library.
28 // The standard Library already provides a set of predefined format.
29 TimestampFormat string
30
31 // DisableTimestamp allows disabling automatic timestamps in output
32 DisableTimestamp bool
33
34 // DisableHTMLEscape allows disabling html escaping in output
35 DisableHTMLEscape bool
36
37 // DataKey allows users to put all the log entry parameters into a nested dictionary at a given key.
38 DataKey string
39
40 // FieldMap allows users to customize the names of keys for default fields.
41 // As an example:
42 // formatter := &JSONFormatter{
43 // FieldMap: FieldMap{
44 // FieldKeyTime: "@timestamp",
45 // FieldKeyLevel: "@level",
46 // FieldKeyMsg: "@message",
47 // FieldKeyFunc: "@caller",
48 // },
49 // }
50 FieldMap FieldMap
51
52 // CallerPrettyfier can be set by the user to modify the content
53 // of the function and file keys in the json data when ReportCaller is
54 // activated. If any of the returned value is the empty string the
55 // corresponding key will be removed from json fields.
56 CallerPrettyfier func(*runtime.Frame) (function string, file string)
57
58 // PrettyPrint will indent all json logs
59 PrettyPrint bool
60}
61
62// Format renders a single log entry
63func (f *JSONFormatter) Format(entry *Entry) ([]byte, error) {
64 data := make(Fields, len(entry.Data)+4)
65 for k, v := range entry.Data {
66 switch v := v.(type) {
67 case error:
68 // Otherwise errors are ignored by `encoding/json`
69 // https://github.com/sirupsen/logrus/issues/137
70 data[k] = v.Error()
71 default:
72 data[k] = v
73 }
74 }
75
76 if f.DataKey != "" {
77 newData := make(Fields, 4)
78 newData[f.DataKey] = data
79 data = newData
80 }
81
82 prefixFieldClashes(data, f.FieldMap, entry.HasCaller())
83
84 timestampFormat := f.TimestampFormat
85 if timestampFormat == "" {
86 timestampFormat = defaultTimestampFormat
87 }
88
89 if entry.err != "" {
90 data[f.FieldMap.resolve(FieldKeyLogrusError)] = entry.err
91 }
92 if !f.DisableTimestamp {
93 data[f.FieldMap.resolve(FieldKeyTime)] = entry.Time.Format(timestampFormat)
94 }
95 data[f.FieldMap.resolve(FieldKeyMsg)] = entry.Message
96 data[f.FieldMap.resolve(FieldKeyLevel)] = entry.Level.String()
97 if entry.HasCaller() {
98 funcVal := entry.Caller.Function
99 fileVal := fmt.Sprintf("%s:%d", entry.Caller.File, entry.Caller.Line)
100 if f.CallerPrettyfier != nil {
101 funcVal, fileVal = f.CallerPrettyfier(entry.Caller)
102 }
103 if funcVal != "" {
104 data[f.FieldMap.resolve(FieldKeyFunc)] = funcVal
105 }
106 if fileVal != "" {
107 data[f.FieldMap.resolve(FieldKeyFile)] = fileVal
108 }
109 }
110
111 var b *bytes.Buffer
112 if entry.Buffer != nil {
113 b = entry.Buffer
114 } else {
115 b = &bytes.Buffer{}
116 }
117
118 encoder := json.NewEncoder(b)
119 encoder.SetEscapeHTML(!f.DisableHTMLEscape)
120 if f.PrettyPrint {
121 encoder.SetIndent("", " ")
122 }
123 if err := encoder.Encode(data); err != nil {
124 return nil, fmt.Errorf("failed to marshal fields to JSON, %w", err)
125 }
126
127 return b.Bytes(), nil
128}
diff --git a/vendor/github.com/sirupsen/logrus/logger.go b/vendor/github.com/sirupsen/logrus/logger.go
new file mode 100644
index 0000000..5ff0aef
--- /dev/null
+++ b/vendor/github.com/sirupsen/logrus/logger.go
@@ -0,0 +1,417 @@
1package logrus
2
3import (
4 "context"
5 "io"
6 "os"
7 "sync"
8 "sync/atomic"
9 "time"
10)
11
12// LogFunction For big messages, it can be more efficient to pass a function
13// and only call it if the log level is actually enables rather than
14// generating the log message and then checking if the level is enabled
15type LogFunction func() []interface{}
16
17type Logger struct {
18 // The logs are `io.Copy`'d to this in a mutex. It's common to set this to a
19 // file, or leave it default which is `os.Stderr`. You can also set this to
20 // something more adventurous, such as logging to Kafka.
21 Out io.Writer
22 // Hooks for the logger instance. These allow firing events based on logging
23 // levels and log entries. For example, to send errors to an error tracking
24 // service, log to StatsD or dump the core on fatal errors.
25 Hooks LevelHooks
26 // All log entries pass through the formatter before logged to Out. The
27 // included formatters are `TextFormatter` and `JSONFormatter` for which
28 // TextFormatter is the default. In development (when a TTY is attached) it
29 // logs with colors, but to a file it wouldn't. You can easily implement your
30 // own that implements the `Formatter` interface, see the `README` or included
31 // formatters for examples.
32 Formatter Formatter
33
34 // Flag for whether to log caller info (off by default)
35 ReportCaller bool
36
37 // The logging level the logger should log at. This is typically (and defaults
38 // to) `logrus.Info`, which allows Info(), Warn(), Error() and Fatal() to be
39 // logged.
40 Level Level
41 // Used to sync writing to the log. Locking is enabled by Default
42 mu MutexWrap
43 // Reusable empty entry
44 entryPool sync.Pool
45 // Function to exit the application, defaults to `os.Exit()`
46 ExitFunc exitFunc
47 // The buffer pool used to format the log. If it is nil, the default global
48 // buffer pool will be used.
49 BufferPool BufferPool
50}
51
52type exitFunc func(int)
53
54type MutexWrap struct {
55 lock sync.Mutex
56 disabled bool
57}
58
59func (mw *MutexWrap) Lock() {
60 if !mw.disabled {
61 mw.lock.Lock()
62 }
63}
64
65func (mw *MutexWrap) Unlock() {
66 if !mw.disabled {
67 mw.lock.Unlock()
68 }
69}
70
71func (mw *MutexWrap) Disable() {
72 mw.disabled = true
73}
74
75// Creates a new logger. Configuration should be set by changing `Formatter`,
76// `Out` and `Hooks` directly on the default logger instance. You can also just
77// instantiate your own:
78//
79// var log = &logrus.Logger{
80// Out: os.Stderr,
81// Formatter: new(logrus.TextFormatter),
82// Hooks: make(logrus.LevelHooks),
83// Level: logrus.DebugLevel,
84// }
85//
86// It's recommended to make this a global instance called `log`.
87func New() *Logger {
88 return &Logger{
89 Out: os.Stderr,
90 Formatter: new(TextFormatter),
91 Hooks: make(LevelHooks),
92 Level: InfoLevel,
93 ExitFunc: os.Exit,
94 ReportCaller: false,
95 }
96}
97
98func (logger *Logger) newEntry() *Entry {
99 entry, ok := logger.entryPool.Get().(*Entry)
100 if ok {
101 return entry
102 }
103 return NewEntry(logger)
104}
105
106func (logger *Logger) releaseEntry(entry *Entry) {
107 entry.Data = map[string]interface{}{}
108 logger.entryPool.Put(entry)
109}
110
111// WithField allocates a new entry and adds a field to it.
112// Debug, Print, Info, Warn, Error, Fatal or Panic must be then applied to
113// this new returned entry.
114// If you want multiple fields, use `WithFields`.
115func (logger *Logger) WithField(key string, value interface{}) *Entry {
116 entry := logger.newEntry()
117 defer logger.releaseEntry(entry)
118 return entry.WithField(key, value)
119}
120
121// Adds a struct of fields to the log entry. All it does is call `WithField` for
122// each `Field`.
123func (logger *Logger) WithFields(fields Fields) *Entry {
124 entry := logger.newEntry()
125 defer logger.releaseEntry(entry)
126 return entry.WithFields(fields)
127}
128
129// Add an error as single field to the log entry. All it does is call
130// `WithError` for the given `error`.
131func (logger *Logger) WithError(err error) *Entry {
132 entry := logger.newEntry()
133 defer logger.releaseEntry(entry)
134 return entry.WithError(err)
135}
136
137// Add a context to the log entry.
138func (logger *Logger) WithContext(ctx context.Context) *Entry {
139 entry := logger.newEntry()
140 defer logger.releaseEntry(entry)
141 return entry.WithContext(ctx)
142}
143
144// Overrides the time of the log entry.
145func (logger *Logger) WithTime(t time.Time) *Entry {
146 entry := logger.newEntry()
147 defer logger.releaseEntry(entry)
148 return entry.WithTime(t)
149}
150
151func (logger *Logger) Logf(level Level, format string, args ...interface{}) {
152 if logger.IsLevelEnabled(level) {
153 entry := logger.newEntry()
154 entry.Logf(level, format, args...)
155 logger.releaseEntry(entry)
156 }
157}
158
159func (logger *Logger) Tracef(format string, args ...interface{}) {
160 logger.Logf(TraceLevel, format, args...)
161}
162
163func (logger *Logger) Debugf(format string, args ...interface{}) {
164 logger.Logf(DebugLevel, format, args...)
165}
166
167func (logger *Logger) Infof(format string, args ...interface{}) {
168 logger.Logf(InfoLevel, format, args...)
169}
170
171func (logger *Logger) Printf(format string, args ...interface{}) {
172 entry := logger.newEntry()
173 entry.Printf(format, args...)
174 logger.releaseEntry(entry)
175}
176
177func (logger *Logger) Warnf(format string, args ...interface{}) {
178 logger.Logf(WarnLevel, format, args...)
179}
180
181func (logger *Logger) Warningf(format string, args ...interface{}) {
182 logger.Warnf(format, args...)
183}
184
185func (logger *Logger) Errorf(format string, args ...interface{}) {
186 logger.Logf(ErrorLevel, format, args...)
187}
188
189func (logger *Logger) Fatalf(format string, args ...interface{}) {
190 logger.Logf(FatalLevel, format, args...)
191 logger.Exit(1)
192}
193
194func (logger *Logger) Panicf(format string, args ...interface{}) {
195 logger.Logf(PanicLevel, format, args...)
196}
197
198// Log will log a message at the level given as parameter.
199// Warning: using Log at Panic or Fatal level will not respectively Panic nor Exit.
200// For this behaviour Logger.Panic or Logger.Fatal should be used instead.
201func (logger *Logger) Log(level Level, args ...interface{}) {
202 if logger.IsLevelEnabled(level) {
203 entry := logger.newEntry()
204 entry.Log(level, args...)
205 logger.releaseEntry(entry)
206 }
207}
208
209func (logger *Logger) LogFn(level Level, fn LogFunction) {
210 if logger.IsLevelEnabled(level) {
211 entry := logger.newEntry()
212 entry.Log(level, fn()...)
213 logger.releaseEntry(entry)
214 }
215}
216
217func (logger *Logger) Trace(args ...interface{}) {
218 logger.Log(TraceLevel, args...)
219}
220
221func (logger *Logger) Debug(args ...interface{}) {
222 logger.Log(DebugLevel, args...)
223}
224
225func (logger *Logger) Info(args ...interface{}) {
226 logger.Log(InfoLevel, args...)
227}
228
229func (logger *Logger) Print(args ...interface{}) {
230 entry := logger.newEntry()
231 entry.Print(args...)
232 logger.releaseEntry(entry)
233}
234
235func (logger *Logger) Warn(args ...interface{}) {
236 logger.Log(WarnLevel, args...)
237}
238
239func (logger *Logger) Warning(args ...interface{}) {
240 logger.Warn(args...)
241}
242
243func (logger *Logger) Error(args ...interface{}) {
244 logger.Log(ErrorLevel, args...)
245}
246
247func (logger *Logger) Fatal(args ...interface{}) {
248 logger.Log(FatalLevel, args...)
249 logger.Exit(1)
250}
251
252func (logger *Logger) Panic(args ...interface{}) {
253 logger.Log(PanicLevel, args...)
254}
255
256func (logger *Logger) TraceFn(fn LogFunction) {
257 logger.LogFn(TraceLevel, fn)
258}
259
260func (logger *Logger) DebugFn(fn LogFunction) {
261 logger.LogFn(DebugLevel, fn)
262}
263
264func (logger *Logger) InfoFn(fn LogFunction) {
265 logger.LogFn(InfoLevel, fn)
266}
267
268func (logger *Logger) PrintFn(fn LogFunction) {
269 entry := logger.newEntry()
270 entry.Print(fn()...)
271 logger.releaseEntry(entry)
272}
273
274func (logger *Logger) WarnFn(fn LogFunction) {
275 logger.LogFn(WarnLevel, fn)
276}
277
278func (logger *Logger) WarningFn(fn LogFunction) {
279 logger.WarnFn(fn)
280}
281
282func (logger *Logger) ErrorFn(fn LogFunction) {
283 logger.LogFn(ErrorLevel, fn)
284}
285
286func (logger *Logger) FatalFn(fn LogFunction) {
287 logger.LogFn(FatalLevel, fn)
288 logger.Exit(1)
289}
290
291func (logger *Logger) PanicFn(fn LogFunction) {
292 logger.LogFn(PanicLevel, fn)
293}
294
295func (logger *Logger) Logln(level Level, args ...interface{}) {
296 if logger.IsLevelEnabled(level) {
297 entry := logger.newEntry()
298 entry.Logln(level, args...)
299 logger.releaseEntry(entry)
300 }
301}
302
303func (logger *Logger) Traceln(args ...interface{}) {
304 logger.Logln(TraceLevel, args...)
305}
306
307func (logger *Logger) Debugln(args ...interface{}) {
308 logger.Logln(DebugLevel, args...)
309}
310
311func (logger *Logger) Infoln(args ...interface{}) {
312 logger.Logln(InfoLevel, args...)
313}
314
315func (logger *Logger) Println(args ...interface{}) {
316 entry := logger.newEntry()
317 entry.Println(args...)
318 logger.releaseEntry(entry)
319}
320
321func (logger *Logger) Warnln(args ...interface{}) {
322 logger.Logln(WarnLevel, args...)
323}
324
325func (logger *Logger) Warningln(args ...interface{}) {
326 logger.Warnln(args...)
327}
328
329func (logger *Logger) Errorln(args ...interface{}) {
330 logger.Logln(ErrorLevel, args...)
331}
332
333func (logger *Logger) Fatalln(args ...interface{}) {
334 logger.Logln(FatalLevel, args...)
335 logger.Exit(1)
336}
337
338func (logger *Logger) Panicln(args ...interface{}) {
339 logger.Logln(PanicLevel, args...)
340}
341
342func (logger *Logger) Exit(code int) {
343 runHandlers()
344 if logger.ExitFunc == nil {
345 logger.ExitFunc = os.Exit
346 }
347 logger.ExitFunc(code)
348}
349
350//When file is opened with appending mode, it's safe to
351//write concurrently to a file (within 4k message on Linux).
352//In these cases user can choose to disable the lock.
353func (logger *Logger) SetNoLock() {
354 logger.mu.Disable()
355}
356
357func (logger *Logger) level() Level {
358 return Level(atomic.LoadUint32((*uint32)(&logger.Level)))
359}
360
361// SetLevel sets the logger level.
362func (logger *Logger) SetLevel(level Level) {
363 atomic.StoreUint32((*uint32)(&logger.Level), uint32(level))
364}
365
366// GetLevel returns the logger level.
367func (logger *Logger) GetLevel() Level {
368 return logger.level()
369}
370
371// AddHook adds a hook to the logger hooks.
372func (logger *Logger) AddHook(hook Hook) {
373 logger.mu.Lock()
374 defer logger.mu.Unlock()
375 logger.Hooks.Add(hook)
376}
377
378// IsLevelEnabled checks if the log level of the logger is greater than the level param
379func (logger *Logger) IsLevelEnabled(level Level) bool {
380 return logger.level() >= level
381}
382
383// SetFormatter sets the logger formatter.
384func (logger *Logger) SetFormatter(formatter Formatter) {
385 logger.mu.Lock()
386 defer logger.mu.Unlock()
387 logger.Formatter = formatter
388}
389
390// SetOutput sets the logger output.
391func (logger *Logger) SetOutput(output io.Writer) {
392 logger.mu.Lock()
393 defer logger.mu.Unlock()
394 logger.Out = output
395}
396
397func (logger *Logger) SetReportCaller(reportCaller bool) {
398 logger.mu.Lock()
399 defer logger.mu.Unlock()
400 logger.ReportCaller = reportCaller
401}
402
403// ReplaceHooks replaces the logger hooks and returns the old ones
404func (logger *Logger) ReplaceHooks(hooks LevelHooks) LevelHooks {
405 logger.mu.Lock()
406 oldHooks := logger.Hooks
407 logger.Hooks = hooks
408 logger.mu.Unlock()
409 return oldHooks
410}
411
412// SetBufferPool sets the logger buffer pool.
413func (logger *Logger) SetBufferPool(pool BufferPool) {
414 logger.mu.Lock()
415 defer logger.mu.Unlock()
416 logger.BufferPool = pool
417}
diff --git a/vendor/github.com/sirupsen/logrus/logrus.go b/vendor/github.com/sirupsen/logrus/logrus.go
new file mode 100644
index 0000000..2f16224
--- /dev/null
+++ b/vendor/github.com/sirupsen/logrus/logrus.go
@@ -0,0 +1,186 @@
1package logrus
2
3import (
4 "fmt"
5 "log"
6 "strings"
7)
8
9// Fields type, used to pass to `WithFields`.
10type Fields map[string]interface{}
11
12// Level type
13type Level uint32
14
15// Convert the Level to a string. E.g. PanicLevel becomes "panic".
16func (level Level) String() string {
17 if b, err := level.MarshalText(); err == nil {
18 return string(b)
19 } else {
20 return "unknown"
21 }
22}
23
24// ParseLevel takes a string level and returns the Logrus log level constant.
25func ParseLevel(lvl string) (Level, error) {
26 switch strings.ToLower(lvl) {
27 case "panic":
28 return PanicLevel, nil
29 case "fatal":
30 return FatalLevel, nil
31 case "error":
32 return ErrorLevel, nil
33 case "warn", "warning":
34 return WarnLevel, nil
35 case "info":
36 return InfoLevel, nil
37 case "debug":
38 return DebugLevel, nil
39 case "trace":
40 return TraceLevel, nil
41 }
42
43 var l Level
44 return l, fmt.Errorf("not a valid logrus Level: %q", lvl)
45}
46
47// UnmarshalText implements encoding.TextUnmarshaler.
48func (level *Level) UnmarshalText(text []byte) error {
49 l, err := ParseLevel(string(text))
50 if err != nil {
51 return err
52 }
53
54 *level = l
55
56 return nil
57}
58
59func (level Level) MarshalText() ([]byte, error) {
60 switch level {
61 case TraceLevel:
62 return []byte("trace"), nil
63 case DebugLevel:
64 return []byte("debug"), nil
65 case InfoLevel:
66 return []byte("info"), nil
67 case WarnLevel:
68 return []byte("warning"), nil
69 case ErrorLevel:
70 return []byte("error"), nil
71 case FatalLevel:
72 return []byte("fatal"), nil
73 case PanicLevel:
74 return []byte("panic"), nil
75 }
76
77 return nil, fmt.Errorf("not a valid logrus level %d", level)
78}
79
80// A constant exposing all logging levels
81var AllLevels = []Level{
82 PanicLevel,
83 FatalLevel,
84 ErrorLevel,
85 WarnLevel,
86 InfoLevel,
87 DebugLevel,
88 TraceLevel,
89}
90
91// These are the different logging levels. You can set the logging level to log
92// on your instance of logger, obtained with `logrus.New()`.
93const (
94 // PanicLevel level, highest level of severity. Logs and then calls panic with the
95 // message passed to Debug, Info, ...
96 PanicLevel Level = iota
97 // FatalLevel level. Logs and then calls `logger.Exit(1)`. It will exit even if the
98 // logging level is set to Panic.
99 FatalLevel
100 // ErrorLevel level. Logs. Used for errors that should definitely be noted.
101 // Commonly used for hooks to send errors to an error tracking service.
102 ErrorLevel
103 // WarnLevel level. Non-critical entries that deserve eyes.
104 WarnLevel
105 // InfoLevel level. General operational entries about what's going on inside the
106 // application.
107 InfoLevel
108 // DebugLevel level. Usually only enabled when debugging. Very verbose logging.
109 DebugLevel
110 // TraceLevel level. Designates finer-grained informational events than the Debug.
111 TraceLevel
112)
113
114// Won't compile if StdLogger can't be realized by a log.Logger
115var (
116 _ StdLogger = &log.Logger{}
117 _ StdLogger = &Entry{}
118 _ StdLogger = &Logger{}
119)
120
121// StdLogger is what your logrus-enabled library should take, that way
122// it'll accept a stdlib logger and a logrus logger. There's no standard
123// interface, this is the closest we get, unfortunately.
124type StdLogger interface {
125 Print(...interface{})
126 Printf(string, ...interface{})
127 Println(...interface{})
128
129 Fatal(...interface{})
130 Fatalf(string, ...interface{})
131 Fatalln(...interface{})
132
133 Panic(...interface{})
134 Panicf(string, ...interface{})
135 Panicln(...interface{})
136}
137
138// The FieldLogger interface generalizes the Entry and Logger types
139type FieldLogger interface {
140 WithField(key string, value interface{}) *Entry
141 WithFields(fields Fields) *Entry
142 WithError(err error) *Entry
143
144 Debugf(format string, args ...interface{})
145 Infof(format string, args ...interface{})
146 Printf(format string, args ...interface{})
147 Warnf(format string, args ...interface{})
148 Warningf(format string, args ...interface{})
149 Errorf(format string, args ...interface{})
150 Fatalf(format string, args ...interface{})
151 Panicf(format string, args ...interface{})
152
153 Debug(args ...interface{})
154 Info(args ...interface{})
155 Print(args ...interface{})
156 Warn(args ...interface{})
157 Warning(args ...interface{})
158 Error(args ...interface{})
159 Fatal(args ...interface{})
160 Panic(args ...interface{})
161
162 Debugln(args ...interface{})
163 Infoln(args ...interface{})
164 Println(args ...interface{})
165 Warnln(args ...interface{})
166 Warningln(args ...interface{})
167 Errorln(args ...interface{})
168 Fatalln(args ...interface{})
169 Panicln(args ...interface{})
170
171 // IsDebugEnabled() bool
172 // IsInfoEnabled() bool
173 // IsWarnEnabled() bool
174 // IsErrorEnabled() bool
175 // IsFatalEnabled() bool
176 // IsPanicEnabled() bool
177}
178
179// Ext1FieldLogger (the first extension to FieldLogger) is superfluous, it is
180// here for consistancy. Do not use. Use Logger or Entry instead.
181type Ext1FieldLogger interface {
182 FieldLogger
183 Tracef(format string, args ...interface{})
184 Trace(args ...interface{})
185 Traceln(args ...interface{})
186}
diff --git a/vendor/github.com/sirupsen/logrus/terminal_check_appengine.go b/vendor/github.com/sirupsen/logrus/terminal_check_appengine.go
new file mode 100644
index 0000000..2403de9
--- /dev/null
+++ b/vendor/github.com/sirupsen/logrus/terminal_check_appengine.go
@@ -0,0 +1,11 @@
1// +build appengine
2
3package logrus
4
5import (
6 "io"
7)
8
9func checkIfTerminal(w io.Writer) bool {
10 return true
11}
diff --git a/vendor/github.com/sirupsen/logrus/terminal_check_bsd.go b/vendor/github.com/sirupsen/logrus/terminal_check_bsd.go
new file mode 100644
index 0000000..4997899
--- /dev/null
+++ b/vendor/github.com/sirupsen/logrus/terminal_check_bsd.go
@@ -0,0 +1,13 @@
1// +build darwin dragonfly freebsd netbsd openbsd
2// +build !js
3
4package logrus
5
6import "golang.org/x/sys/unix"
7
8const ioctlReadTermios = unix.TIOCGETA
9
10func isTerminal(fd int) bool {
11 _, err := unix.IoctlGetTermios(fd, ioctlReadTermios)
12 return err == nil
13}
diff --git a/vendor/github.com/sirupsen/logrus/terminal_check_js.go b/vendor/github.com/sirupsen/logrus/terminal_check_js.go
new file mode 100644
index 0000000..ebdae3e
--- /dev/null
+++ b/vendor/github.com/sirupsen/logrus/terminal_check_js.go
@@ -0,0 +1,7 @@
1// +build js
2
3package logrus
4
5func isTerminal(fd int) bool {
6 return false
7}
diff --git a/vendor/github.com/sirupsen/logrus/terminal_check_no_terminal.go b/vendor/github.com/sirupsen/logrus/terminal_check_no_terminal.go
new file mode 100644
index 0000000..97af92c
--- /dev/null
+++ b/vendor/github.com/sirupsen/logrus/terminal_check_no_terminal.go
@@ -0,0 +1,11 @@
1// +build js nacl plan9
2
3package logrus
4
5import (
6 "io"
7)
8
9func checkIfTerminal(w io.Writer) bool {
10 return false
11}
diff --git a/vendor/github.com/sirupsen/logrus/terminal_check_notappengine.go b/vendor/github.com/sirupsen/logrus/terminal_check_notappengine.go
new file mode 100644
index 0000000..3293fb3
--- /dev/null
+++ b/vendor/github.com/sirupsen/logrus/terminal_check_notappengine.go
@@ -0,0 +1,17 @@
1// +build !appengine,!js,!windows,!nacl,!plan9
2
3package logrus
4
5import (
6 "io"
7 "os"
8)
9
10func checkIfTerminal(w io.Writer) bool {
11 switch v := w.(type) {
12 case *os.File:
13 return isTerminal(int(v.Fd()))
14 default:
15 return false
16 }
17}
diff --git a/vendor/github.com/sirupsen/logrus/terminal_check_solaris.go b/vendor/github.com/sirupsen/logrus/terminal_check_solaris.go
new file mode 100644
index 0000000..f6710b3
--- /dev/null
+++ b/vendor/github.com/sirupsen/logrus/terminal_check_solaris.go
@@ -0,0 +1,11 @@
1package logrus
2
3import (
4 "golang.org/x/sys/unix"
5)
6
7// IsTerminal returns true if the given file descriptor is a terminal.
8func isTerminal(fd int) bool {
9 _, err := unix.IoctlGetTermio(fd, unix.TCGETA)
10 return err == nil
11}
diff --git a/vendor/github.com/sirupsen/logrus/terminal_check_unix.go b/vendor/github.com/sirupsen/logrus/terminal_check_unix.go
new file mode 100644
index 0000000..04748b8
--- /dev/null
+++ b/vendor/github.com/sirupsen/logrus/terminal_check_unix.go
@@ -0,0 +1,13 @@
1// +build linux aix zos
2// +build !js
3
4package logrus
5
6import "golang.org/x/sys/unix"
7
8const ioctlReadTermios = unix.TCGETS
9
10func isTerminal(fd int) bool {
11 _, err := unix.IoctlGetTermios(fd, ioctlReadTermios)
12 return err == nil
13}
diff --git a/vendor/github.com/sirupsen/logrus/terminal_check_windows.go b/vendor/github.com/sirupsen/logrus/terminal_check_windows.go
new file mode 100644
index 0000000..2879eb5
--- /dev/null
+++ b/vendor/github.com/sirupsen/logrus/terminal_check_windows.go
@@ -0,0 +1,27 @@
1// +build !appengine,!js,windows
2
3package logrus
4
5import (
6 "io"
7 "os"
8
9 "golang.org/x/sys/windows"
10)
11
12func checkIfTerminal(w io.Writer) bool {
13 switch v := w.(type) {
14 case *os.File:
15 handle := windows.Handle(v.Fd())
16 var mode uint32
17 if err := windows.GetConsoleMode(handle, &mode); err != nil {
18 return false
19 }
20 mode |= windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING
21 if err := windows.SetConsoleMode(handle, mode); err != nil {
22 return false
23 }
24 return true
25 }
26 return false
27}
diff --git a/vendor/github.com/sirupsen/logrus/text_formatter.go b/vendor/github.com/sirupsen/logrus/text_formatter.go
new file mode 100644
index 0000000..be2c6ef
--- /dev/null
+++ b/vendor/github.com/sirupsen/logrus/text_formatter.go
@@ -0,0 +1,339 @@
1package logrus
2
3import (
4 "bytes"
5 "fmt"
6 "os"
7 "runtime"
8 "sort"
9 "strconv"
10 "strings"
11 "sync"
12 "time"
13 "unicode/utf8"
14)
15
16const (
17 red = 31
18 yellow = 33
19 blue = 36
20 gray = 37
21)
22
23var baseTimestamp time.Time
24
25func init() {
26 baseTimestamp = time.Now()
27}
28
29// TextFormatter formats logs into text
30type TextFormatter struct {
31 // Set to true to bypass checking for a TTY before outputting colors.
32 ForceColors bool
33
34 // Force disabling colors.
35 DisableColors bool
36
37 // Force quoting of all values
38 ForceQuote bool
39
40 // DisableQuote disables quoting for all values.
41 // DisableQuote will have a lower priority than ForceQuote.
42 // If both of them are set to true, quote will be forced on all values.
43 DisableQuote bool
44
45 // Override coloring based on CLICOLOR and CLICOLOR_FORCE. - https://bixense.com/clicolors/
46 EnvironmentOverrideColors bool
47
48 // Disable timestamp logging. useful when output is redirected to logging
49 // system that already adds timestamps.
50 DisableTimestamp bool
51
52 // Enable logging the full timestamp when a TTY is attached instead of just
53 // the time passed since beginning of execution.
54 FullTimestamp bool
55
56 // TimestampFormat to use for display when a full timestamp is printed.
57 // The format to use is the same than for time.Format or time.Parse from the standard
58 // library.
59 // The standard Library already provides a set of predefined format.
60 TimestampFormat string
61
62 // The fields are sorted by default for a consistent output. For applications
63 // that log extremely frequently and don't use the JSON formatter this may not
64 // be desired.
65 DisableSorting bool
66
67 // The keys sorting function, when uninitialized it uses sort.Strings.
68 SortingFunc func([]string)
69
70 // Disables the truncation of the level text to 4 characters.
71 DisableLevelTruncation bool
72
73 // PadLevelText Adds padding the level text so that all the levels output at the same length
74 // PadLevelText is a superset of the DisableLevelTruncation option
75 PadLevelText bool
76
77 // QuoteEmptyFields will wrap empty fields in quotes if true
78 QuoteEmptyFields bool
79
80 // Whether the logger's out is to a terminal
81 isTerminal bool
82
83 // FieldMap allows users to customize the names of keys for default fields.
84 // As an example:
85 // formatter := &TextFormatter{
86 // FieldMap: FieldMap{
87 // FieldKeyTime: "@timestamp",
88 // FieldKeyLevel: "@level",
89 // FieldKeyMsg: "@message"}}
90 FieldMap FieldMap
91
92 // CallerPrettyfier can be set by the user to modify the content
93 // of the function and file keys in the data when ReportCaller is
94 // activated. If any of the returned value is the empty string the
95 // corresponding key will be removed from fields.
96 CallerPrettyfier func(*runtime.Frame) (function string, file string)
97
98 terminalInitOnce sync.Once
99
100 // The max length of the level text, generated dynamically on init
101 levelTextMaxLength int
102}
103
104func (f *TextFormatter) init(entry *Entry) {
105 if entry.Logger != nil {
106 f.isTerminal = checkIfTerminal(entry.Logger.Out)
107 }
108 // Get the max length of the level text
109 for _, level := range AllLevels {
110 levelTextLength := utf8.RuneCount([]byte(level.String()))
111 if levelTextLength > f.levelTextMaxLength {
112 f.levelTextMaxLength = levelTextLength
113 }
114 }
115}
116
117func (f *TextFormatter) isColored() bool {
118 isColored := f.ForceColors || (f.isTerminal && (runtime.GOOS != "windows"))
119
120 if f.EnvironmentOverrideColors {
121 switch force, ok := os.LookupEnv("CLICOLOR_FORCE"); {
122 case ok && force != "0":
123 isColored = true
124 case ok && force == "0", os.Getenv("CLICOLOR") == "0":
125 isColored = false
126 }
127 }
128
129 return isColored && !f.DisableColors
130}
131
132// Format renders a single log entry
133func (f *TextFormatter) Format(entry *Entry) ([]byte, error) {
134 data := make(Fields)
135 for k, v := range entry.Data {
136 data[k] = v
137 }
138 prefixFieldClashes(data, f.FieldMap, entry.HasCaller())
139 keys := make([]string, 0, len(data))
140 for k := range data {
141 keys = append(keys, k)
142 }
143
144 var funcVal, fileVal string
145
146 fixedKeys := make([]string, 0, 4+len(data))
147 if !f.DisableTimestamp {
148 fixedKeys = append(fixedKeys, f.FieldMap.resolve(FieldKeyTime))
149 }
150 fixedKeys = append(fixedKeys, f.FieldMap.resolve(FieldKeyLevel))
151 if entry.Message != "" {
152 fixedKeys = append(fixedKeys, f.FieldMap.resolve(FieldKeyMsg))
153 }
154 if entry.err != "" {
155 fixedKeys = append(fixedKeys, f.FieldMap.resolve(FieldKeyLogrusError))
156 }
157 if entry.HasCaller() {
158 if f.CallerPrettyfier != nil {
159 funcVal, fileVal = f.CallerPrettyfier(entry.Caller)
160 } else {
161 funcVal = entry.Caller.Function
162 fileVal = fmt.Sprintf("%s:%d", entry.Caller.File, entry.Caller.Line)
163 }
164
165 if funcVal != "" {
166 fixedKeys = append(fixedKeys, f.FieldMap.resolve(FieldKeyFunc))
167 }
168 if fileVal != "" {
169 fixedKeys = append(fixedKeys, f.FieldMap.resolve(FieldKeyFile))
170 }
171 }
172
173 if !f.DisableSorting {
174 if f.SortingFunc == nil {
175 sort.Strings(keys)
176 fixedKeys = append(fixedKeys, keys...)
177 } else {
178 if !f.isColored() {
179 fixedKeys = append(fixedKeys, keys...)
180 f.SortingFunc(fixedKeys)
181 } else {
182 f.SortingFunc(keys)
183 }
184 }
185 } else {
186 fixedKeys = append(fixedKeys, keys...)
187 }
188
189 var b *bytes.Buffer
190 if entry.Buffer != nil {
191 b = entry.Buffer
192 } else {
193 b = &bytes.Buffer{}
194 }
195
196 f.terminalInitOnce.Do(func() { f.init(entry) })
197
198 timestampFormat := f.TimestampFormat
199 if timestampFormat == "" {
200 timestampFormat = defaultTimestampFormat
201 }
202 if f.isColored() {
203 f.printColored(b, entry, keys, data, timestampFormat)
204 } else {
205
206 for _, key := range fixedKeys {
207 var value interface{}
208 switch {
209 case key == f.FieldMap.resolve(FieldKeyTime):
210 value = entry.Time.Format(timestampFormat)
211 case key == f.FieldMap.resolve(FieldKeyLevel):
212 value = entry.Level.String()
213 case key == f.FieldMap.resolve(FieldKeyMsg):
214 value = entry.Message
215 case key == f.FieldMap.resolve(FieldKeyLogrusError):
216 value = entry.err
217 case key == f.FieldMap.resolve(FieldKeyFunc) && entry.HasCaller():
218 value = funcVal
219 case key == f.FieldMap.resolve(FieldKeyFile) && entry.HasCaller():
220 value = fileVal
221 default:
222 value = data[key]
223 }
224 f.appendKeyValue(b, key, value)
225 }
226 }
227
228 b.WriteByte('\n')
229 return b.Bytes(), nil
230}
231
232func (f *TextFormatter) printColored(b *bytes.Buffer, entry *Entry, keys []string, data Fields, timestampFormat string) {
233 var levelColor int
234 switch entry.Level {
235 case DebugLevel, TraceLevel:
236 levelColor = gray
237 case WarnLevel:
238 levelColor = yellow
239 case ErrorLevel, FatalLevel, PanicLevel:
240 levelColor = red
241 case InfoLevel:
242 levelColor = blue
243 default:
244 levelColor = blue
245 }
246
247 levelText := strings.ToUpper(entry.Level.String())
248 if !f.DisableLevelTruncation && !f.PadLevelText {
249 levelText = levelText[0:4]
250 }
251 if f.PadLevelText {
252 // Generates the format string used in the next line, for example "%-6s" or "%-7s".
253 // Based on the max level text length.
254 formatString := "%-" + strconv.Itoa(f.levelTextMaxLength) + "s"
255 // Formats the level text by appending spaces up to the max length, for example:
256 // - "INFO "
257 // - "WARNING"
258 levelText = fmt.Sprintf(formatString, levelText)
259 }
260
261 // Remove a single newline if it already exists in the message to keep
262 // the behavior of logrus text_formatter the same as the stdlib log package
263 entry.Message = strings.TrimSuffix(entry.Message, "\n")
264
265 caller := ""
266 if entry.HasCaller() {
267 funcVal := fmt.Sprintf("%s()", entry.Caller.Function)
268 fileVal := fmt.Sprintf("%s:%d", entry.Caller.File, entry.Caller.Line)
269
270 if f.CallerPrettyfier != nil {
271 funcVal, fileVal = f.CallerPrettyfier(entry.Caller)
272 }
273
274 if fileVal == "" {
275 caller = funcVal
276 } else if funcVal == "" {
277 caller = fileVal
278 } else {
279 caller = fileVal + " " + funcVal
280 }
281 }
282
283 switch {
284 case f.DisableTimestamp:
285 fmt.Fprintf(b, "\x1b[%dm%s\x1b[0m%s %-44s ", levelColor, levelText, caller, entry.Message)
286 case !f.FullTimestamp:
287 fmt.Fprintf(b, "\x1b[%dm%s\x1b[0m[%04d]%s %-44s ", levelColor, levelText, int(entry.Time.Sub(baseTimestamp)/time.Second), caller, entry.Message)
288 default:
289 fmt.Fprintf(b, "\x1b[%dm%s\x1b[0m[%s]%s %-44s ", levelColor, levelText, entry.Time.Format(timestampFormat), caller, entry.Message)
290 }
291 for _, k := range keys {
292 v := data[k]
293 fmt.Fprintf(b, " \x1b[%dm%s\x1b[0m=", levelColor, k)
294 f.appendValue(b, v)
295 }
296}
297
298func (f *TextFormatter) needsQuoting(text string) bool {
299 if f.ForceQuote {
300 return true
301 }
302 if f.QuoteEmptyFields && len(text) == 0 {
303 return true
304 }
305 if f.DisableQuote {
306 return false
307 }
308 for _, ch := range text {
309 if !((ch >= 'a' && ch <= 'z') ||
310 (ch >= 'A' && ch <= 'Z') ||
311 (ch >= '0' && ch <= '9') ||
312 ch == '-' || ch == '.' || ch == '_' || ch == '/' || ch == '@' || ch == '^' || ch == '+') {
313 return true
314 }
315 }
316 return false
317}
318
319func (f *TextFormatter) appendKeyValue(b *bytes.Buffer, key string, value interface{}) {
320 if b.Len() > 0 {
321 b.WriteByte(' ')
322 }
323 b.WriteString(key)
324 b.WriteByte('=')
325 f.appendValue(b, value)
326}
327
328func (f *TextFormatter) appendValue(b *bytes.Buffer, value interface{}) {
329 stringVal, ok := value.(string)
330 if !ok {
331 stringVal = fmt.Sprint(value)
332 }
333
334 if !f.needsQuoting(stringVal) {
335 b.WriteString(stringVal)
336 } else {
337 b.WriteString(fmt.Sprintf("%q", stringVal))
338 }
339}
diff --git a/vendor/github.com/sirupsen/logrus/writer.go b/vendor/github.com/sirupsen/logrus/writer.go
new file mode 100644
index 0000000..074fd4b
--- /dev/null
+++ b/vendor/github.com/sirupsen/logrus/writer.go
@@ -0,0 +1,102 @@
1package logrus
2
3import (
4 "bufio"
5 "io"
6 "runtime"
7 "strings"
8)
9
10// Writer at INFO level. See WriterLevel for details.
11func (logger *Logger) Writer() *io.PipeWriter {
12 return logger.WriterLevel(InfoLevel)
13}
14
15// WriterLevel returns an io.Writer that can be used to write arbitrary text to
16// the logger at the given log level. Each line written to the writer will be
17// printed in the usual way using formatters and hooks. The writer is part of an
18// io.Pipe and it is the callers responsibility to close the writer when done.
19// This can be used to override the standard library logger easily.
20func (logger *Logger) WriterLevel(level Level) *io.PipeWriter {
21 return NewEntry(logger).WriterLevel(level)
22}
23
24// Writer returns an io.Writer that writes to the logger at the info log level
25func (entry *Entry) Writer() *io.PipeWriter {
26 return entry.WriterLevel(InfoLevel)
27}
28
29// WriterLevel returns an io.Writer that writes to the logger at the given log level
30func (entry *Entry) WriterLevel(level Level) *io.PipeWriter {
31 reader, writer := io.Pipe()
32
33 var printFunc func(args ...interface{})
34
35 // Determine which log function to use based on the specified log level
36 switch level {
37 case TraceLevel:
38 printFunc = entry.Trace
39 case DebugLevel:
40 printFunc = entry.Debug
41 case InfoLevel:
42 printFunc = entry.Info
43 case WarnLevel:
44 printFunc = entry.Warn
45 case ErrorLevel:
46 printFunc = entry.Error
47 case FatalLevel:
48 printFunc = entry.Fatal
49 case PanicLevel:
50 printFunc = entry.Panic
51 default:
52 printFunc = entry.Print
53 }
54
55 // Start a new goroutine to scan the input and write it to the logger using the specified print function.
56 // It splits the input into chunks of up to 64KB to avoid buffer overflows.
57 go entry.writerScanner(reader, printFunc)
58
59 // Set a finalizer function to close the writer when it is garbage collected
60 runtime.SetFinalizer(writer, writerFinalizer)
61
62 return writer
63}
64
65// writerScanner scans the input from the reader and writes it to the logger
66func (entry *Entry) writerScanner(reader *io.PipeReader, printFunc func(args ...interface{})) {
67 scanner := bufio.NewScanner(reader)
68
69 // Set the buffer size to the maximum token size to avoid buffer overflows
70 scanner.Buffer(make([]byte, bufio.MaxScanTokenSize), bufio.MaxScanTokenSize)
71
72 // Define a split function to split the input into chunks of up to 64KB
73 chunkSize := bufio.MaxScanTokenSize // 64KB
74 splitFunc := func(data []byte, atEOF bool) (int, []byte, error) {
75 if len(data) >= chunkSize {
76 return chunkSize, data[:chunkSize], nil
77 }
78
79 return bufio.ScanLines(data, atEOF)
80 }
81
82 // Use the custom split function to split the input
83 scanner.Split(splitFunc)
84
85 // Scan the input and write it to the logger using the specified print function
86 for scanner.Scan() {
87 printFunc(strings.TrimRight(scanner.Text(), "\r\n"))
88 }
89
90 // If there was an error while scanning the input, log an error
91 if err := scanner.Err(); err != nil {
92 entry.Errorf("Error while reading from Writer: %s", err)
93 }
94
95 // Close the reader when we are done
96 reader.Close()
97}
98
99// WriterFinalizer is a finalizer function that closes then given writer when it is garbage collected
100func writerFinalizer(writer *io.PipeWriter) {
101 writer.Close()
102}