Skip to content

Commit 29b2337

Browse files
authored
Merge pull request #1468 from thaJeztah/touchup_readme
README: small touch-ups
2 parents d916819 + 135e482 commit 29b2337

File tree

1 file changed

+54
-50
lines changed

1 file changed

+54
-50
lines changed

README.md

Lines changed: 54 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ plain text):
4040

4141
![Colored](http://i.imgur.com/PY7qMwd.png)
4242

43-
With `log.SetFormatter(&log.JSONFormatter{})`, for easy parsing by logstash
43+
With `logrus.SetFormatter(&logrus.JSONFormatter{})`, for easy parsing by logstash
4444
or Splunk:
4545

4646
```text
@@ -60,7 +60,7 @@ ocean","size":10,"time":"2014-03-10 19:57:38.562264131 -0400 EDT"}
6060
"time":"2014-03-10 19:57:38.562543128 -0400 EDT"}
6161
```
6262

63-
With the default `log.SetFormatter(&log.TextFormatter{})` when a TTY is not
63+
With the default `logrus.SetFormatter(&logrus.TextFormatter{})` when a TTY is not
6464
attached, the output is compatible with the
6565
[logfmt](https://pkg.go.dev/github.com/kr/logfmt) format:
6666

@@ -75,17 +75,18 @@ time="2015-03-26T01:27:38-04:00" level=fatal msg="The ice breaks!" err=&{0x20822
7575
To ensure this behaviour even if a TTY is attached, set your formatter as follows:
7676

7777
```go
78-
log.SetFormatter(&log.TextFormatter{
79-
DisableColors: true,
80-
FullTimestamp: true,
81-
})
78+
logrus.SetFormatter(&logrus.TextFormatter{
79+
DisableColors: true,
80+
FullTimestamp: true,
81+
})
8282
```
8383

8484
#### Logging Method Name
8585

8686
If you wish to add the calling method as a field, instruct the logger via:
87+
8788
```go
88-
log.SetReportCaller(true)
89+
logrus.SetReportCaller(true)
8990
```
9091
This adds the caller as 'method' like so:
9192

@@ -100,11 +101,11 @@ time="2015-03-26T01:27:38-04:00" level=fatal method=github.com/sirupsen/arcticcr
100101
Note that this does add measurable overhead - the cost will depend on the version of Go, but is
101102
between 20 and 40% in recent tests with 1.6 and 1.7. You can validate this in your
102103
environment via benchmarks:
103-
```
104+
105+
```bash
104106
go test -bench=.*CallerTracing
105107
```
106108

107-
108109
#### Case-sensitivity
109110

110111
The organization's name was changed to lower-case--and this will not be changed
@@ -118,12 +119,10 @@ The simplest way to use Logrus is simply the package-level exported logger:
118119
```go
119120
package main
120121

121-
import (
122-
log "github.com/sirupsen/logrus"
123-
)
122+
import "github.com/sirupsen/logrus"
124123

125124
func main() {
126-
log.WithFields(log.Fields{
125+
logrus.WithFields(logrus.Fields{
127126
"animal": "walrus",
128127
}).Info("A walrus appears")
129128
}
@@ -139,6 +138,7 @@ package main
139138

140139
import (
141140
"os"
141+
142142
log "github.com/sirupsen/logrus"
143143
)
144144

@@ -190,26 +190,27 @@ package main
190190

191191
import (
192192
"os"
193+
193194
"github.com/sirupsen/logrus"
194195
)
195196

196197
// Create a new instance of the logger. You can have any number of instances.
197-
var log = logrus.New()
198+
var logger = logrus.New()
198199

199200
func main() {
200201
// The API for setting attributes is a little different than the package level
201-
// exported logger. See Godoc.
202-
log.Out = os.Stdout
202+
// exported logger. See Godoc.
203+
logger.Out = os.Stdout
203204

204205
// You could set this to any `io.Writer` such as a file
205206
// file, err := os.OpenFile("logrus.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
206207
// if err == nil {
207-
// log.Out = file
208+
// logger.Out = file
208209
// } else {
209-
// log.Info("Failed to log to file, using default stderr")
210+
// logger.Info("Failed to log to file, using default stderr")
210211
// }
211212

212-
log.WithFields(logrus.Fields{
213+
logger.WithFields(logrus.Fields{
213214
"animal": "walrus",
214215
"size": 10,
215216
}).Info("A group of walrus emerges from the ocean")
@@ -219,12 +220,12 @@ func main() {
219220
#### Fields
220221

221222
Logrus encourages careful, structured logging through logging fields instead of
222-
long, unparseable error messages. For example, instead of: `log.Fatalf("Failed
223+
long, unparseable error messages. For example, instead of: `logrus.Fatalf("Failed
223224
to send event %s to topic %s with key %d")`, you should log the much more
224225
discoverable:
225226

226227
```go
227-
log.WithFields(log.Fields{
228+
logrus.WithFields(logrus.Fields{
228229
"event": event,
229230
"topic": topic,
230231
"key": key,
@@ -245,12 +246,12 @@ seen as a hint you should add a field, however, you can still use the
245246
Often it's helpful to have fields _always_ attached to log statements in an
246247
application or parts of one. For example, you may want to always log the
247248
`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
249+
`logger.WithFields(logrus.Fields{"request_id": request_id, "user_ip": user_ip})` on
249250
every line, you can create a `logrus.Entry` to pass around instead:
250251

251252
```go
252-
requestLogger := log.WithFields(log.Fields{"request_id": request_id, "user_ip": user_ip})
253-
requestLogger.Info("something happened on that request") # will log request_id and user_ip
253+
requestLogger := logger.WithFields(logrus.Fields{"request_id": request_id, "user_ip": user_ip})
254+
requestLogger.Info("something happened on that request") // will log request_id and user_ip
254255
requestLogger.Warn("something not great happened")
255256
```
256257

@@ -264,24 +265,27 @@ Logrus comes with [built-in hooks](hooks/). Add those, or your custom hook, in
264265
`init`:
265266

266267
```go
268+
package main
269+
267270
import (
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"
271271
"log/syslog"
272+
273+
"github.com/sirupsen/logrus"
274+
airbrake "gopkg.in/gemnasium/logrus-airbrake-hook.v2"
275+
logrus_syslog "github.com/sirupsen/logrus/hooks/syslog"
272276
)
273277

274278
func init() {
275279

276280
// Use the Airbrake hook to report errors that have Error severity or above to
277281
// an exception tracker. You can create custom hooks, see the Hooks section.
278-
log.AddHook(airbrake.NewHook(123, "xyz", "production"))
282+
logrus.AddHook(airbrake.NewHook(123, "xyz", "production"))
279283

280284
hook, err := logrus_syslog.NewSyslogHook("udp", "localhost:514", syslog.LOG_INFO, "")
281285
if err != nil {
282-
log.Error("Unable to connect to local syslog daemon")
286+
logrus.Error("Unable to connect to local syslog daemon")
283287
} else {
284-
log.AddHook(hook)
288+
logrus.AddHook(hook)
285289
}
286290
}
287291
```
@@ -295,29 +299,29 @@ A list of currently known service hooks can be found in this wiki [page](https:/
295299
Logrus has seven logging levels: Trace, Debug, Info, Warning, Error, Fatal and Panic.
296300

297301
```go
298-
log.Trace("Something very low level.")
299-
log.Debug("Useful debugging information.")
300-
log.Info("Something noteworthy happened!")
301-
log.Warn("You should probably take a look at this.")
302-
log.Error("Something failed but I'm not quitting.")
302+
logrus.Trace("Something very low level.")
303+
logrus.Debug("Useful debugging information.")
304+
logrus.Info("Something noteworthy happened!")
305+
logrus.Warn("You should probably take a look at this.")
306+
logrus.Error("Something failed but I'm not quitting.")
303307
// Calls os.Exit(1) after logging
304-
log.Fatal("Bye.")
308+
logrus.Fatal("Bye.")
305309
// Calls panic() after logging
306-
log.Panic("I'm bailing.")
310+
logrus.Panic("I'm bailing.")
307311
```
308312

309313
You can set the logging level on a `Logger`, then it will only log entries with
310314
that severity or anything above it:
311315

312316
```go
313317
// Will log anything that is info or above (warn, error, fatal, panic). Default.
314-
log.SetLevel(log.InfoLevel)
318+
logrus.SetLevel(logrus.InfoLevel)
315319
```
316320

317-
It may be useful to set `log.Level = logrus.DebugLevel` in a debug or verbose
321+
It may be useful to set `logrus.Level = logrus.DebugLevel` in a debug or verbose
318322
environment if your application has that.
319323

320-
Note: 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).
324+
Note: If you want different log levels for global (`logrus.SetLevel(...)`) and syslog logging, please check the [syslog hook README](hooks/syslog/README.md#different-log-levels-for-local-and-remote-logging).
321325

322326
#### Entries
323327

@@ -340,17 +344,17 @@ could do:
340344

341345
```go
342346
import (
343-
log "github.com/sirupsen/logrus"
347+
"github.com/sirupsen/logrus"
344348
)
345349

346350
func init() {
347351
// do something here to set environment depending on an environment variable
348352
// or command-line flag
349353
if Environment == "production" {
350-
log.SetFormatter(&log.JSONFormatter{})
354+
logrus.SetFormatter(&logrus.JSONFormatter{})
351355
} else {
352356
// The TextFormatter is default, you don't actually have to do this.
353-
log.SetFormatter(&log.TextFormatter{})
357+
logrus.SetFormatter(&logrus.TextFormatter{})
354358
}
355359
}
356360
```
@@ -393,10 +397,9 @@ requiring a `Format` method. `Format` takes an `*Entry`. `entry.Data` is a
393397
default ones (see Entries section above):
394398

395399
```go
396-
type MyJSONFormatter struct {
397-
}
400+
type MyJSONFormatter struct{}
398401

399-
log.SetFormatter(new(MyJSONFormatter))
402+
logrus.SetFormatter(new(MyJSONFormatter))
400403

401404
func (f *MyJSONFormatter) Format(entry *Entry) ([]byte, error) {
402405
// Note this doesn't include Time, Level and Message which are available on
@@ -462,10 +465,11 @@ Logrus has a built-in facility for asserting the presence of log messages. This
462465

463466
```go
464467
import(
468+
"testing"
469+
465470
"github.com/sirupsen/logrus"
466471
"github.com/sirupsen/logrus/hooks/test"
467472
"github.com/stretchr/testify/assert"
468-
"testing"
469473
)
470474

471475
func TestSomething(t*testing.T){
@@ -488,13 +492,13 @@ level message is logged. The registered handlers will be executed before
488492
logrus performs an `os.Exit(1)`. This behavior may be helpful if callers need
489493
to gracefully shut down. 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.
490494

491-
```
492-
...
495+
```go
496+
// ...
493497
handler := func() {
494498
// gracefully shut down something...
495499
}
496500
logrus.RegisterExitHandler(handler)
497-
...
501+
// ...
498502
```
499503

500504
#### Thread safety

0 commit comments

Comments
 (0)