Compare commits

..

1 Commits

Author SHA1 Message Date
Mark Phelps cc70782477 add mutex on entry, not logging the global mutex 2020-04-16 09:20:41 -04:00
47 changed files with 232 additions and 811 deletions

20
.github/stale.yml vendored Normal file
View File

@ -0,0 +1,20 @@
# Number of days of inactivity before an issue becomes stale
daysUntilStale: 60
# Number of days of inactivity before a stale issue is closed
daysUntilClose: 14
# Issues with these labels will never be considered stale
exemptLabels:
- question
- bug
- documentation
# Label to use when marking an issue as stale
staleLabel: stale
# Comment to post when marking an issue as stale. Set to `false` to disable
markComment: >
This issue has been automatically marked as stale because it has not had
recent activity. It will be closed if no further activity occurs. Thank you
for your contributions.
# Comment to post when closing a stale issue. Set to `false` to disable
closeComment: false
# Only mark issues as stale for now until we go through backlog of PRs
only: issues

View File

@ -1,61 +0,0 @@
name: CI
on:
push:
branches:
- master
pull_request:
branches:
- master
jobs:
lint:
name: Golang-CI Lint
timeout-minutes: 10
strategy:
matrix:
platform: [ubuntu-latest]
runs-on: ${{ matrix.platform }}
steps:
- uses: actions/checkout@v2
- uses: golangci/golangci-lint-action@v2
with:
# must be specified without patch version
version: v1.46
cross:
name: Cross
timeout-minutes: 10
strategy:
matrix:
go-version: [1.17.x]
platform: [ubuntu-latest]
runs-on: ${{ matrix.platform }}
steps:
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go-version }}
- name: Checkout code
uses: actions/checkout@v2
- name: Cross
working-directory: ci
run: go run mage.go -v -w ../ crossBuild
test:
name: Unit test
timeout-minutes: 10
strategy:
matrix:
go-version: [1.17.x]
platform: [ubuntu-latest, windows-latest]
runs-on: ${{ matrix.platform }}
steps:
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go-version }}
- name: Checkout code
uses: actions/checkout@v2
- name: Test
run: go test -race -v ./...

View File

@ -1,22 +0,0 @@
name: Close inactive issues
on:
schedule:
- cron: "30 1 * * *"
jobs:
close-issues:
runs-on: ubuntu-latest
permissions:
issues: write
pull-requests: write
steps:
- uses: actions/stale@v3
with:
days-before-issue-stale: 30
days-before-issue-close: 14
stale-issue-label: "stale"
stale-issue-message: "This issue is stale because it has been open for 30 days with no activity."
close-issue-message: "This issue was closed because it has been inactive for 14 days since being marked as stale."
days-before-pr-stale: -1
days-before-pr-close: -1
repo-token: ${{ secrets.GITHUB_TOKEN }}

2
.gitignore vendored
View File

@ -1,4 +1,2 @@
logrus logrus
vendor vendor
.idea/

View File

@ -1,15 +1,17 @@
language: go language: go
go_import_path: git.internal/re/logrus go_import_path: github.com/sirupsen/logrus
git: git:
depth: 1 depth: 1
env: env:
- GO111MODULE=on - GO111MODULE=on
go: 1.15.x go: [1.13.x, 1.14.x]
os: linux os: [linux, osx]
install: install:
- ./travis/install.sh - ./travis/install.sh
script: script:
- cd ci - ./travis/cross_build.sh
- go run mage.go -v -w ../ crossBuild - ./travis/lint.sh
- go run mage.go -v -w ../ lint - export GOMAXPROCS=4
- go run mage.go -v -w ../ test - export GORACE=halt_on_error=1
- go test -race -v ./...
- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then go test -race -v -tags appengine ./... ; fi

View File

@ -1,62 +1,3 @@
# 1.8.1
Code quality:
* move magefile in its own subdir/submodule to remove magefile dependency on logrus consumer
* improve timestamp format documentation
Fixes:
* fix race condition on logger hooks
# 1.8.0
Correct versioning number replacing v1.7.1.
# 1.7.1
Beware this release has introduced a new public API and its semver is therefore incorrect.
Code quality:
* use go 1.15 in travis
* use magefile as task runner
Fixes:
* small fixes about new go 1.13 error formatting system
* Fix for long time race condiction with mutating data hooks
Features:
* build support for zos
# 1.7.0
Fixes:
* the dependency toward a windows terminal library has been removed
Features:
* a new buffer pool management API has been added
* a set of `<LogLevel>Fn()` functions have been added
# 1.6.0
Fixes:
* end of line cleanup
* revert the entry concurrency bug fix whic leads to deadlock under some circumstances
* update dependency on go-windows-terminal-sequences to fix a crash with go 1.14
Features:
* add an option to the `TextFormatter` to completely disable fields quoting
# 1.5.0
Code quality:
* add golangci linter run on travis
Fixes:
* add mutex for hooks concurrent access on `Entry` data
* caller function field for go1.14
* fix build issue for gopherjs target
Feature:
* add an hooks/writer sub-package whose goal is to split output on different stream depending on the trace level
* add a `DisableHTMLEscape` option in the `JSONFormatter`
* add `ForceQuote` and `PadLevelText` options in the `TextFormatter`
# 1.4.2 # 1.4.2
* Fixes build break for plan9, nacl, solaris * Fixes build break for plan9, nacl, solaris
# 1.4.1 # 1.4.1
@ -70,7 +11,7 @@ Fixes:
# 1.4.0 # 1.4.0
This new release introduces: This new release introduces:
* Add `DeferExitHandler`, similar to `RegisterExitHandler` but prepending the handler to the list of handlers (semantically like `defer`) (#848). * Add `DeferExitHandler`, similar to `RegisterExitHandler` but prepending the handler to the list of handlers (semantically like `defer`) (#848).
* Add `CallerPrettyfier` to `JSONFormatter` and `TextFormatter` (#909, #911) * Add `CallerPrettyfier` to `JSONFormatter` and `TextFormatter (#909, #911)
* Add `Entry.WithContext()` and `Entry.Context`, to set a context on entries to be used e.g. in hooks (#919). * Add `Entry.WithContext()` and `Entry.Context`, to set a context on entries to be used e.g. in hooks (#919).
Fixes: Fixes:

View File

@ -1,4 +1,4 @@
# Logrus <img src="http://i.imgur.com/hTeVwmJ.png" width="40" height="40" alt=":walrus:" class="emoji" title=":walrus:"/> [![Build Status](https://git.internal/re/logrus/workflows/CI/badge.svg)](https://git.internal/re/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/git.internal/re/logrus.svg)](https://pkg.go.dev/git.internal/re/logrus) # Logrus <img src="http://i.imgur.com/hTeVwmJ.png" width="40" height="40" alt=":walrus:" class="emoji" title=":walrus:"/> [![Build Status](https://travis-ci.org/sirupsen/logrus.svg?branch=master)](https://travis-ci.org/sirupsen/logrus) [![GoDoc](https://godoc.org/github.com/sirupsen/logrus?status.svg)](https://godoc.org/github.com/sirupsen/logrus)
Logrus is a structured logger for Go (golang), completely API compatible with Logrus is a structured logger for Go (golang), completely API compatible with
the standard library logger. the standard library logger.
@ -28,12 +28,12 @@ import Logrus as both upper- and lower-case. Due to the Go package environment,
this caused issues in the community and we needed a standard. Some environments this caused issues in the community and we needed a standard. Some environments
experienced problems with the upper-case variant, so the lower-case was decided. experienced problems with the upper-case variant, so the lower-case was decided.
Everything using `logrus` will need to use the lower-case: Everything using `logrus` will need to use the lower-case:
`git.internal/re/logrus`. Any package that isn't, should be changed. `github.com/sirupsen/logrus`. Any package that isn't, should be changed.
To fix Glide, see [these To fix Glide, see [these
comments](https://git.internal/re/logrus/issues/553#issuecomment-306591437). comments](https://github.com/sirupsen/logrus/issues/553#issuecomment-306591437).
For an in-depth explanation of the casing issue, see [this For an in-depth explanation of the casing issue, see [this
comment](https://git.internal/re/logrus/issues/570#issuecomment-313933276). comment](https://github.com/sirupsen/logrus/issues/570#issuecomment-313933276).
Nicely color-coded in development (when a TTY is attached, otherwise just Nicely color-coded in development (when a TTY is attached, otherwise just
plain text): plain text):
@ -109,7 +109,7 @@ go test -bench=.*CallerTracing
The organization's name was changed to lower-case--and this will not be changed The organization's name was changed to lower-case--and this will not be changed
back. If you are getting import conflicts due to case sensitivity, please use back. If you are getting import conflicts due to case sensitivity, please use
the lower-case import: `git.internal/re/logrus`. the lower-case import: `github.com/sirupsen/logrus`.
#### Example #### Example
@ -119,7 +119,7 @@ The simplest way to use Logrus is simply the package-level exported logger:
package main package main
import ( import (
log "git.internal/re/logrus" log "github.com/sirupsen/logrus"
) )
func main() { func main() {
@ -130,7 +130,7 @@ func main() {
``` ```
Note that it's completely api-compatible with the stdlib logger, so you can Note that it's completely api-compatible with the stdlib logger, so you can
replace your `log` imports everywhere with `log "git.internal/re/logrus"` replace your `log` imports everywhere with `log "github.com/sirupsen/logrus"`
and you'll now have the flexibility of Logrus. You can customize it all you and you'll now have the flexibility of Logrus. You can customize it all you
want: want:
@ -139,7 +139,7 @@ package main
import ( import (
"os" "os"
log "git.internal/re/logrus" log "github.com/sirupsen/logrus"
) )
func init() { func init() {
@ -190,7 +190,7 @@ package main
import ( import (
"os" "os"
"git.internal/re/logrus" "github.com/sirupsen/logrus"
) )
// Create a new instance of the logger. You can have any number of instances. // Create a new instance of the logger. You can have any number of instances.
@ -265,9 +265,9 @@ Logrus comes with [built-in hooks](hooks/). Add those, or your custom hook, in
```go ```go
import ( import (
log "git.internal/re/logrus" log "github.com/sirupsen/logrus"
"gopkg.in/gemnasium/logrus-airbrake-hook.v2" // the package is named "airbrake" "gopkg.in/gemnasium/logrus-airbrake-hook.v2" // the package is named "airbrake"
logrus_syslog "git.internal/re/logrus/hooks/syslog" logrus_syslog "github.com/sirupsen/logrus/hooks/syslog"
"log/syslog" "log/syslog"
) )
@ -287,7 +287,7 @@ func init() {
``` ```
Note: Syslog hook also support connecting to local syslog (Ex. "/dev/log" or "/var/run/syslog" or "/var/run/log"). For the detail, please check the [syslog hook README](hooks/syslog/README.md). Note: Syslog hook also support connecting to local syslog (Ex. "/dev/log" or "/var/run/syslog" or "/var/run/log"). For the detail, please check the [syslog hook README](hooks/syslog/README.md).
A list of currently known service hooks can be found in this wiki [page](https://git.internal/re/logrus/wiki/Hooks) A list of currently known service hooks can be found in this wiki [page](https://github.com/sirupsen/logrus/wiki/Hooks)
#### Level logging #### Level logging
@ -338,10 +338,10 @@ could do:
```go ```go
import ( import (
log "git.internal/re/logrus" log "github.com/sirupsen/logrus"
) )
func init() { init() {
// do something here to set environment depending on an environment variable // do something here to set environment depending on an environment variable
// or command-line flag // or command-line flag
if Environment == "production" { if Environment == "production" {
@ -370,9 +370,9 @@ The built-in logging formatters are:
* When colors are enabled, levels are truncated to 4 characters by default. To disable * When colors are enabled, levels are truncated to 4 characters by default. To disable
truncation set the `DisableLevelTruncation` field to `true`. truncation set the `DisableLevelTruncation` field to `true`.
* 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. * 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.
* All options are listed in the [generated docs](https://godoc.org/git.internal/re/logrus#TextFormatter). * All options are listed in the [generated docs](https://godoc.org/github.com/sirupsen/logrus#TextFormatter).
* `logrus.JSONFormatter`. Logs fields as JSON. * `logrus.JSONFormatter`. Logs fields as JSON.
* All options are listed in the [generated docs](https://godoc.org/git.internal/re/logrus#JSONFormatter). * All options are listed in the [generated docs](https://godoc.org/github.com/sirupsen/logrus#JSONFormatter).
Third party logging formatters: Third party logging formatters:
@ -402,7 +402,7 @@ func (f *MyJSONFormatter) Format(entry *Entry) ([]byte, error) {
// source of the official loggers. // source of the official loggers.
serialized, err := json.Marshal(entry.Data) serialized, err := json.Marshal(entry.Data)
if err != nil { if err != nil {
return nil, fmt.Errorf("Failed to marshal fields to JSON, %w", err) return nil, fmt.Errorf("Failed to marshal fields to JSON, %v", err)
} }
return append(serialized, '\n'), nil return append(serialized, '\n'), nil
} }
@ -460,8 +460,8 @@ Logrus has a built in facility for asserting the presence of log messages. This
```go ```go
import( import(
"git.internal/re/logrus" "github.com/sirupsen/logrus"
"git.internal/re/logrus/hooks/test" "github.com/sirupsen/logrus/hooks/test"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"testing" "testing"
) )

View File

@ -1,43 +0,0 @@
package logrus
import (
"bytes"
"sync"
)
var (
bufferPool BufferPool
)
type BufferPool interface {
Put(*bytes.Buffer)
Get() *bytes.Buffer
}
type defaultPool struct {
pool *sync.Pool
}
func (p *defaultPool) Put(buf *bytes.Buffer) {
p.pool.Put(buf)
}
func (p *defaultPool) Get() *bytes.Buffer {
return p.pool.Get().(*bytes.Buffer)
}
// SetBufferPool allows to replace the default logrus buffer pool
// to better meets the specific needs of an application.
func SetBufferPool(bp BufferPool) {
bufferPool = bp
}
func init() {
SetBufferPool(&defaultPool{
pool: &sync.Pool{
New: func() interface{} {
return new(bytes.Buffer)
},
},
})
}

View File

@ -1,5 +0,0 @@
module git.internal/re/logrus/ci
go 1.15
require github.com/magefile/mage v1.11.0

View File

@ -1,2 +0,0 @@
github.com/magefile/mage v1.11.0 h1:C/55Ywp9BpgVVclD3lRnSYCwXTYxmSppIgLeDYlNuls=
github.com/magefile/mage v1.11.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A=

View File

@ -1,10 +0,0 @@
// +build ignore
package main
import (
"github.com/magefile/mage/mage"
"os"
)
func main() { os.Exit(mage.Main()) }

View File

@ -1,123 +0,0 @@
//go:build mage
package main
import (
"encoding/json"
"fmt"
"os"
"path"
"sort"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
)
func intersect(a, b []string) []string {
sort.Strings(a)
sort.Strings(b)
res := make([]string, 0, func() int {
if len(a) < len(b) {
return len(a)
}
return len(b)
}())
for _, v := range a {
idx := sort.SearchStrings(b, v)
if idx < len(b) && b[idx] == v {
res = append(res, v)
}
}
return res
}
// getBuildMatrix returns the build matrix from the current version of the go compiler
func getFullBuildMatrix() (map[string][]string, error) {
jsonData, err := sh.Output("go", "tool", "dist", "list", "-json")
if err != nil {
return nil, err
}
var data []struct {
Goos string
Goarch string
}
if err := json.Unmarshal([]byte(jsonData), &data); err != nil {
return nil, err
}
matrix := map[string][]string{}
for _, v := range data {
if val, ok := matrix[v.Goos]; ok {
matrix[v.Goos] = append(val, v.Goarch)
} else {
matrix[v.Goos] = []string{v.Goarch}
}
}
return matrix, nil
}
func getBuildMatrix() (map[string][]string, error) {
minimalMatrix := map[string][]string{
"linux": []string{"amd64"},
"darwin": []string{"amd64", "arm64"},
"freebsd": []string{"amd64"},
"js": []string{"wasm"},
"solaris": []string{"amd64"},
"windows": []string{"amd64", "arm64"},
}
fullMatrix, err := getFullBuildMatrix()
if err != nil {
return nil, err
}
for os, arches := range minimalMatrix {
if fullV, ok := fullMatrix[os]; !ok {
delete(minimalMatrix, os)
} else {
minimalMatrix[os] = intersect(arches, fullV)
}
}
return minimalMatrix, nil
}
func CrossBuild() error {
matrix, err := getBuildMatrix()
if err != nil {
return err
}
for os, arches := range matrix {
for _, arch := range arches {
env := map[string]string{
"GOOS": os,
"GOARCH": arch,
}
if mg.Verbose() {
fmt.Printf("Building for GOOS=%s GOARCH=%s\n", os, arch)
}
if err := sh.RunWith(env, "go", "build", "./..."); err != nil {
return err
}
}
}
return nil
}
func Lint() error {
gopath := os.Getenv("GOPATH")
if gopath == "" {
return fmt.Errorf("cannot retrieve GOPATH")
}
return sh.Run(path.Join(gopath, "bin", "golangci-lint"), "run", "./...")
}
// Run the test suite
func Test() error {
return sh.RunWith(map[string]string{"GORACE": "halt_on_error=1"},
"go", "test", "-race", "-v", "./...")
}

6
doc.go
View File

@ -1,12 +1,13 @@
/* /*
Package logrus is a structured logger for Go, completely API compatible with the standard library logger. Package logrus is a structured logger for Go, completely API compatible with the standard library logger.
The simplest way to use Logrus is simply the package-level exported logger: The simplest way to use Logrus is simply the package-level exported logger:
package main package main
import ( import (
log "git.internal/re/logrus" log "github.com/sirupsen/logrus"
) )
func main() { func main() {
@ -18,9 +19,8 @@ The simplest way to use Logrus is simply the package-level exported logger:
} }
Output: Output:
time="2015-09-07T08:48:33Z" level=info msg="A walrus appears" animal=walrus number=1 size=10 time="2015-09-07T08:48:33Z" level=info msg="A walrus appears" animal=walrus number=1 size=10
For a full guide visit https://git.internal/re/logrus For a full guide visit https://github.com/sirupsen/logrus
*/ */
package logrus package logrus

View File

@ -13,6 +13,7 @@ import (
) )
var ( var (
bufferPool *sync.Pool
// qualified package name, cached at first use // qualified package name, cached at first use
logrusPackage string logrusPackage string
@ -30,6 +31,12 @@ const (
) )
func init() { func init() {
bufferPool = &sync.Pool{
New: func() interface{} {
return new(bytes.Buffer)
},
}
// start at the bottom of the stack before the package-name cache is primed // start at the bottom of the stack before the package-name cache is primed
minimumCallerDepth = 1 minimumCallerDepth = 1
} }
@ -68,6 +75,8 @@ type Entry struct {
// err may contain a field formatting error // err may contain a field formatting error
err string err string
mu sync.Mutex
} }
func NewEntry(logger *Logger) *Entry { func NewEntry(logger *Logger) *Entry {
@ -78,14 +87,6 @@ func NewEntry(logger *Logger) *Entry {
} }
} }
func (entry *Entry) Dup() *Entry {
data := make(Fields, len(entry.Data))
for k, v := range entry.Data {
data[k] = v
}
return &Entry{Logger: entry.Logger, Data: data, Time: entry.Time, Context: entry.Context, err: entry.err}
}
// Returns the bytes representation of this entry from the formatter. // Returns the bytes representation of this entry from the formatter.
func (entry *Entry) Bytes() ([]byte, error) { func (entry *Entry) Bytes() ([]byte, error) {
return entry.Logger.Formatter.Format(entry) return entry.Logger.Formatter.Format(entry)
@ -123,6 +124,8 @@ func (entry *Entry) WithField(key string, value interface{}) *Entry {
// Add a map of fields to the Entry. // Add a map of fields to the Entry.
func (entry *Entry) WithFields(fields Fields) *Entry { func (entry *Entry) WithFields(fields Fields) *Entry {
entry.mu.Lock()
defer entry.mu.Unlock()
data := make(Fields, len(entry.Data)+len(fields)) data := make(Fields, len(entry.Data)+len(fields))
for k, v := range entry.Data { for k, v := range entry.Data {
data[k] = v data[k] = v
@ -131,9 +134,11 @@ func (entry *Entry) WithFields(fields Fields) *Entry {
for k, v := range fields { for k, v := range fields {
isErrField := false isErrField := false
if t := reflect.TypeOf(v); t != nil { if t := reflect.TypeOf(v); t != nil {
switch { switch t.Kind() {
case t.Kind() == reflect.Func, t.Kind() == reflect.Ptr && t.Elem().Kind() == reflect.Func: case reflect.Func:
isErrField = true isErrField = true
case reflect.Ptr:
isErrField = t.Elem().Kind() == reflect.Func
} }
} }
if isErrField { if isErrField {
@ -218,66 +223,51 @@ func (entry Entry) HasCaller() (has bool) {
entry.Caller != nil entry.Caller != nil
} }
func (entry *Entry) log(level Level, msg string) { // This function is not declared with a pointer value because otherwise
// race conditions will occur when using multiple goroutines
func (entry Entry) log(level Level, msg string) {
var buffer *bytes.Buffer var buffer *bytes.Buffer
newEntry := entry.Dup() // Default to now, but allow users to override if they want.
//
if newEntry.Time.IsZero() { // We don't have to worry about polluting future calls to Entry#log()
newEntry.Time = time.Now() // with this assignment because this function is declared with a
// non-pointer receiver.
if entry.Time.IsZero() {
entry.Time = time.Now()
} }
newEntry.Level = level entry.Level = level
newEntry.Message = msg entry.Message = msg
entry.Logger.mu.Lock()
newEntry.Logger.mu.Lock() if entry.Logger.ReportCaller {
reportCaller := newEntry.Logger.ReportCaller entry.Caller = getCaller()
bufPool := newEntry.getBufferPool()
newEntry.Logger.mu.Unlock()
if reportCaller {
newEntry.Caller = getCaller()
} }
entry.Logger.mu.Unlock()
newEntry.fireHooks() entry.fireHooks()
buffer = bufPool.Get()
defer func() { buffer = bufferPool.Get().(*bytes.Buffer)
newEntry.Buffer = nil
buffer.Reset() buffer.Reset()
bufPool.Put(buffer) defer bufferPool.Put(buffer)
}() entry.Buffer = buffer
buffer.Reset()
newEntry.Buffer = buffer
newEntry.write() entry.write()
newEntry.Buffer = nil entry.Buffer = nil
// To avoid Entry#log() returning a value that only would make sense for // To avoid Entry#log() returning a value that only would make sense for
// panic() to use in Entry#Panic(), we avoid the allocation by checking // panic() to use in Entry#Panic(), we avoid the allocation by checking
// directly here. // directly here.
if level <= PanicLevel { if level <= PanicLevel {
panic(newEntry) panic(&entry)
} }
} }
func (entry *Entry) getBufferPool() (pool BufferPool) {
if entry.Logger.BufferPool != nil {
return entry.Logger.BufferPool
}
return bufferPool
}
func (entry *Entry) fireHooks() { func (entry *Entry) fireHooks() {
var tmpHooks LevelHooks
entry.Logger.mu.Lock() entry.Logger.mu.Lock()
tmpHooks = make(LevelHooks, len(entry.Logger.Hooks)) defer entry.Logger.mu.Unlock()
for k, v := range entry.Logger.Hooks { err := entry.Logger.Hooks.Fire(entry.Level, entry)
tmpHooks[k] = v
}
entry.Logger.mu.Unlock()
err := tmpHooks.Fire(entry.Level, entry)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "Failed to fire hook: %v\n", err) fmt.Fprintf(os.Stderr, "Failed to fire hook: %v\n", err)
} }
@ -291,14 +281,11 @@ func (entry *Entry) write() {
fmt.Fprintf(os.Stderr, "Failed to obtain reader, %v\n", err) fmt.Fprintf(os.Stderr, "Failed to obtain reader, %v\n", err)
return return
} }
if _, err := entry.Logger.Out.Write(serialized); err != nil { if _, err = entry.Logger.Out.Write(serialized); err != nil {
fmt.Fprintf(os.Stderr, "Failed to write to log, %v\n", err) fmt.Fprintf(os.Stderr, "Failed to write to log, %v\n", err)
} }
} }
// Log will log a message at the level given as parameter.
// Warning: using Log at Panic or Fatal level will not respectively Panic nor Exit.
// For this behaviour Entry.Panic or Entry.Fatal should be used instead.
func (entry *Entry) Log(level Level, args ...interface{}) { func (entry *Entry) Log(level Level, args ...interface{}) {
if entry.Logger.IsLevelEnabled(level) { if entry.Logger.IsLevelEnabled(level) {
entry.log(level, fmt.Sprint(args...)) entry.log(level, fmt.Sprint(args...))
@ -340,6 +327,7 @@ func (entry *Entry) Fatal(args ...interface{}) {
func (entry *Entry) Panic(args ...interface{}) { func (entry *Entry) Panic(args ...interface{}) {
entry.Log(PanicLevel, args...) entry.Log(PanicLevel, args...)
panic(fmt.Sprint(args...))
} }
// Entry Printf family functions // Entry Printf family functions

View File

@ -167,28 +167,6 @@ func TestEntryPanicf(t *testing.T) {
entry.WithField("err", errBoom).Panicf("kaboom %v", true) entry.WithField("err", errBoom).Panicf("kaboom %v", true)
} }
func TestEntryPanic(t *testing.T) {
errBoom := fmt.Errorf("boom again")
defer func() {
p := recover()
assert.NotNil(t, p)
switch pVal := p.(type) {
case *Entry:
assert.Equal(t, "kaboom", pVal.Message)
assert.Equal(t, errBoom, pVal.Data["err"])
default:
t.Fatalf("want type *Entry, got %T: %#v", pVal, pVal)
}
}()
logger := New()
logger.Out = &bytes.Buffer{}
entry := NewEntry(logger)
entry.WithField("err", errBoom).Panic("kaboom")
}
const ( const (
badMessage = "this is going to panic" badMessage = "this is going to panic"
panicMessage = "this is broken" panicMessage = "this is broken"
@ -232,7 +210,7 @@ func TestEntryWithIncorrectField(t *testing.T) {
fn := func() {} fn := func() {}
e := Entry{Logger: New()} e := &Entry{Logger: New()}
eWithFunc := e.WithFields(Fields{"func": fn}) eWithFunc := e.WithFields(Fields{"func": fn})
eWithFuncPtr := e.WithFields(Fields{"funcPtr": &fn}) eWithFuncPtr := e.WithFields(Fields{"funcPtr": &fn})
@ -269,12 +247,6 @@ func TestEntryLogfLevel(t *testing.T) {
func TestEntryReportCallerRace(t *testing.T) { func TestEntryReportCallerRace(t *testing.T) {
logger := New() logger := New()
entry := NewEntry(logger) entry := NewEntry(logger)
// logging before SetReportCaller has the highest chance of causing a race condition
// to be detected, but doing it twice just to increase the likelyhood of detecting the race
go func() {
entry.Info("should not race")
}()
go func() { go func() {
logger.SetReportCaller(true) logger.SetReportCaller(true)
}() }()
@ -282,20 +254,3 @@ func TestEntryReportCallerRace(t *testing.T) {
entry.Info("should not race") entry.Info("should not race")
}() }()
} }
func TestEntryFormatterRace(t *testing.T) {
logger := New()
entry := NewEntry(logger)
// logging before SetReportCaller has the highest chance of causing a race condition
// to be detected, but doing it twice just to increase the likelyhood of detecting the race
go func() {
entry.Info("should not race")
}()
go func() {
logger.SetFormatter(&TextFormatter{})
}()
go func() {
entry.Info("should not race")
}()
}

View File

@ -3,11 +3,11 @@ package logrus_test
import ( import (
"os" "os"
"git.internal/re/logrus" "github.com/sirupsen/logrus"
) )
func Example_basic() { func Example_basic() {
log := logrus.New() var log = logrus.New()
log.Formatter = new(logrus.JSONFormatter) log.Formatter = new(logrus.JSONFormatter)
log.Formatter = new(logrus.TextFormatter) //default log.Formatter = new(logrus.TextFormatter) //default
log.Formatter.(*logrus.TextFormatter).DisableColors = true // remove colors log.Formatter.(*logrus.TextFormatter).DisableColors = true // remove colors

View File

@ -6,7 +6,7 @@ import (
"runtime" "runtime"
"strings" "strings"
"git.internal/re/logrus" "github.com/sirupsen/logrus"
) )
func ExampleJSONFormatter_CallerPrettyfier() { func ExampleJSONFormatter_CallerPrettyfier() {

View File

@ -3,7 +3,7 @@ package logrus_test
import ( import (
"os" "os"
"git.internal/re/logrus" "github.com/sirupsen/logrus"
) )
type DefaultFieldHook struct { type DefaultFieldHook struct {

View File

@ -1,31 +0,0 @@
package logrus_test
import (
"testing"
log "git.internal/re/logrus"
"github.com/stretchr/testify/assert"
)
func TestLogger_LogFn(t *testing.T) {
log.SetFormatter(&log.JSONFormatter{})
log.SetLevel(log.WarnLevel)
notCalled := 0
log.InfoFn(func() []interface{} {
notCalled++
return []interface{}{
"Hello",
}
})
assert.Equal(t, 0, notCalled)
called := 0
log.ErrorFn(func() []interface{} {
called++
return []interface{}{
"Oopsi",
}
})
assert.Equal(t, 1, called)
}

View File

@ -3,12 +3,15 @@ package logrus_test
import ( import (
"os" "os"
"git.internal/re/logrus" "github.com/sirupsen/logrus"
) )
var mystring string var (
mystring string
)
type GlobalHook struct{} type GlobalHook struct {
}
func (h *GlobalHook) Levels() []logrus.Level { func (h *GlobalHook) Levels() []logrus.Level {
return logrus.AllLevels return logrus.AllLevels

View File

@ -1,4 +1,3 @@
//go:build !windows
// +build !windows // +build !windows
package logrus_test package logrus_test
@ -7,13 +6,13 @@ import (
"log/syslog" "log/syslog"
"os" "os"
"git.internal/re/logrus" "github.com/sirupsen/logrus"
slhooks "git.internal/re/logrus/hooks/syslog" slhooks "github.com/sirupsen/logrus/hooks/syslog"
) )
// An example on how to use a hook // An example on how to use a hook
func Example_hook() { func Example_hook() {
log := logrus.New() var log = logrus.New()
log.Formatter = new(logrus.TextFormatter) // default log.Formatter = new(logrus.TextFormatter) // default
log.Formatter.(*logrus.TextFormatter).DisableColors = true // remove colors log.Formatter.(*logrus.TextFormatter).DisableColors = true // remove colors
log.Formatter.(*logrus.TextFormatter).DisableTimestamp = true // remove timestamp from test output log.Formatter.(*logrus.TextFormatter).DisableTimestamp = true // remove timestamp from test output

View File

@ -134,51 +134,6 @@ func Fatal(args ...interface{}) {
std.Fatal(args...) std.Fatal(args...)
} }
// TraceFn logs a message from a func at level Trace on the standard logger.
func TraceFn(fn LogFunction) {
std.TraceFn(fn)
}
// DebugFn logs a message from a func at level Debug on the standard logger.
func DebugFn(fn LogFunction) {
std.DebugFn(fn)
}
// PrintFn logs a message from a func at level Info on the standard logger.
func PrintFn(fn LogFunction) {
std.PrintFn(fn)
}
// InfoFn logs a message from a func at level Info on the standard logger.
func InfoFn(fn LogFunction) {
std.InfoFn(fn)
}
// WarnFn logs a message from a func at level Warn on the standard logger.
func WarnFn(fn LogFunction) {
std.WarnFn(fn)
}
// WarningFn logs a message from a func at level Warn on the standard logger.
func WarningFn(fn LogFunction) {
std.WarningFn(fn)
}
// ErrorFn logs a message from a func at level Error on the standard logger.
func ErrorFn(fn LogFunction) {
std.ErrorFn(fn)
}
// PanicFn logs a message from a func at level Panic on the standard logger.
func PanicFn(fn LogFunction) {
std.PanicFn(fn)
}
// FatalFn logs a message from a func at level Fatal on the standard logger then the process will exit with status set to 1.
func FatalFn(fn LogFunction) {
std.FatalFn(fn)
}
// Tracef logs a message at level Trace on the standard logger. // Tracef logs a message at level Trace on the standard logger.
func Tracef(format string, args ...interface{}) { func Tracef(format string, args ...interface{}) {
std.Tracef(format, args...) std.Tracef(format, args...)

8
go.mod
View File

@ -1,9 +1,11 @@
module git.internal/re/logrus module github.com/sirupsen/logrus
require ( require (
github.com/davecgh/go-spew v1.1.1 // indirect github.com/davecgh/go-spew v1.1.1 // indirect
github.com/stretchr/testify v1.7.0 github.com/konsorten/go-windows-terminal-sequences v1.0.1
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.2.2
golang.org/x/sys v0.0.0-20190422165155-953cdadca894
) )
go 1.13 go 1.13

16
go.sum
View File

@ -1,14 +1,10 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= golang.org/x/sys v0.0.0-20190422165155-953cdadca894 h1:Cz4ceDQGXuKRnVBDTS23GTn/pU5OE2C0WrNTOYK1Uuc=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ= golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View File

@ -3,16 +3,14 @@ package logrus_test
import ( import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"fmt"
"sync" "sync"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
. "git.internal/re/logrus" . "github.com/sirupsen/logrus"
"git.internal/re/logrus/hooks/test" . "github.com/sirupsen/logrus/internal/testutils"
. "git.internal/re/logrus/internal/testutils"
) )
type TestHook struct { type TestHook struct {
@ -49,7 +47,8 @@ func TestHookFires(t *testing.T) {
}) })
} }
type ModifyHook struct{} type ModifyHook struct {
}
func (hook *ModifyHook) Fire(entry *Entry) error { func (hook *ModifyHook) Fire(entry *Entry) error {
entry.Data["wow"] = "whale" entry.Data["wow"] = "whale"
@ -192,20 +191,6 @@ func TestAddHookRace(t *testing.T) {
}) })
} }
func TestAddHookRace2(t *testing.T) {
t.Parallel()
for i := 0; i < 3; i++ {
testname := fmt.Sprintf("Test %d", i)
t.Run(testname, func(t *testing.T) {
t.Parallel()
_ = test.NewGlobal()
Info(testname)
})
}
}
type HookCallFunc struct { type HookCallFunc struct {
F func() F func()
} }

View File

@ -5,8 +5,8 @@
```go ```go
import ( import (
"log/syslog" "log/syslog"
"git.internal/re/logrus" "github.com/sirupsen/logrus"
lSyslog "git.internal/re/logrus/hooks/syslog" lSyslog "github.com/sirupsen/logrus/hooks/syslog"
) )
func main() { func main() {
@ -24,8 +24,8 @@ If you want to connect to local syslog (Ex. "/dev/log" or "/var/run/syslog" or "
```go ```go
import ( import (
"log/syslog" "log/syslog"
"git.internal/re/logrus" "github.com/sirupsen/logrus"
lSyslog "git.internal/re/logrus/hooks/syslog" lSyslog "github.com/sirupsen/logrus/hooks/syslog"
) )
func main() { func main() {

View File

@ -1,4 +1,3 @@
//go:build !windows && !nacl && !plan9
// +build !windows,!nacl,!plan9 // +build !windows,!nacl,!plan9
package syslog package syslog
@ -8,7 +7,7 @@ import (
"log/syslog" "log/syslog"
"os" "os"
"git.internal/re/logrus" "github.com/sirupsen/logrus"
) )
// SyslogHook to send logs via syslog. // SyslogHook to send logs via syslog.

View File

@ -1,4 +1,3 @@
//go:build !windows && !nacl && !plan9
// +build !windows,!nacl,!plan9 // +build !windows,!nacl,!plan9
package syslog package syslog
@ -7,12 +6,13 @@ import (
"log/syslog" "log/syslog"
"testing" "testing"
"git.internal/re/logrus" "github.com/sirupsen/logrus"
) )
func TestLocalhostAddAndPrint(t *testing.T) { func TestLocalhostAddAndPrint(t *testing.T) {
log := logrus.New() log := logrus.New()
hook, err := NewSyslogHook("udp", "localhost:514", syslog.LOG_INFO, "") hook, err := NewSyslogHook("udp", "localhost:514", syslog.LOG_INFO, "")
if err != nil { if err != nil {
t.Errorf("Unable to connect to local syslog.") t.Errorf("Unable to connect to local syslog.")
} }

View File

@ -6,7 +6,7 @@ import (
"io/ioutil" "io/ioutil"
"sync" "sync"
"git.internal/re/logrus" "github.com/sirupsen/logrus"
) )
// Hook is a hook designed for dealing with logs in test scenarios. // Hook is a hook designed for dealing with logs in test scenarios.
@ -20,26 +20,32 @@ type Hook struct {
// NewGlobal installs a test hook for the global logger. // NewGlobal installs a test hook for the global logger.
func NewGlobal() *Hook { func NewGlobal() *Hook {
hook := new(Hook) hook := new(Hook)
logrus.AddHook(hook) logrus.AddHook(hook)
return hook return hook
} }
// NewLocal installs a test hook for a given local logger. // NewLocal installs a test hook for a given local logger.
func NewLocal(logger *logrus.Logger) *Hook { func NewLocal(logger *logrus.Logger) *Hook {
hook := new(Hook) hook := new(Hook)
logger.Hooks.Add(hook) logger.Hooks.Add(hook)
return hook return hook
} }
// NewNullLogger creates a discarding logger and installs the test hook. // NewNullLogger creates a discarding logger and installs the test hook.
func NewNullLogger() (*logrus.Logger, *Hook) { func NewNullLogger() (*logrus.Logger, *Hook) {
logger := logrus.New() logger := logrus.New()
logger.Out = ioutil.Discard logger.Out = ioutil.Discard
return logger, NewLocal(logger) return logger, NewLocal(logger)
} }
func (t *Hook) Fire(e *logrus.Entry) error { func (t *Hook) Fire(e *logrus.Entry) error {

View File

@ -6,7 +6,7 @@ import (
"testing" "testing"
"time" "time"
"git.internal/re/logrus" "github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@ -40,6 +40,7 @@ func TestAllHooks(t *testing.T) {
} }
func TestLoggingWithHooksRace(t *testing.T) { func TestLoggingWithHooksRace(t *testing.T) {
rand.Seed(time.Now().Unix()) rand.Seed(time.Now().Unix())
unlocker := rand.Int() % 100 unlocker := rand.Int() % 100

View File

@ -14,8 +14,8 @@ import (
"io/ioutil" "io/ioutil"
"os" "os"
log "git.internal/re/logrus" log "github.com/sirupsen/logrus"
"git.internal/re/logrus/hooks/writer" "github.com/sirupsen/logrus/hooks/writer"
) )
func main() { func main() {

View File

@ -3,7 +3,7 @@ package writer
import ( import (
"io" "io"
log "git.internal/re/logrus" log "github.com/sirupsen/logrus"
) )
// Hook is a hook that writes logs of specified LogLevels to specified Writer // Hook is a hook that writes logs of specified LogLevels to specified Writer

View File

@ -5,7 +5,7 @@ import (
"io/ioutil" "io/ioutil"
"testing" "testing"
log "git.internal/re/logrus" log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )

View File

@ -7,7 +7,7 @@ import (
"strings" "strings"
"testing" "testing"
. "git.internal/re/logrus" . "github.com/sirupsen/logrus"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )

View File

@ -23,9 +23,6 @@ func (f FieldMap) resolve(key fieldKey) string {
// JSONFormatter formats logs into parsable json // JSONFormatter formats logs into parsable json
type JSONFormatter struct { type JSONFormatter struct {
// TimestampFormat sets the format used for marshaling timestamps. // TimestampFormat sets the format used for marshaling timestamps.
// The format to use is the same than for time.Format or time.Parse from the standard
// library.
// The standard Library already provides a set of predefined format.
TimestampFormat string TimestampFormat string
// DisableTimestamp allows disabling automatic timestamps in output // DisableTimestamp allows disabling automatic timestamps in output
@ -66,7 +63,7 @@ func (f *JSONFormatter) Format(entry *Entry) ([]byte, error) {
switch v := v.(type) { switch v := v.(type) {
case error: case error:
// Otherwise errors are ignored by `encoding/json` // Otherwise errors are ignored by `encoding/json`
// https://git.internal/re/logrus/issues/137 // https://github.com/sirupsen/logrus/issues/137
data[k] = v.Error() data[k] = v.Error()
default: default:
data[k] = v data[k] = v
@ -121,7 +118,7 @@ func (f *JSONFormatter) Format(entry *Entry) ([]byte, error) {
encoder.SetIndent("", " ") encoder.SetIndent("", " ")
} }
if err := encoder.Encode(data); err != nil { if err := encoder.Encode(data); err != nil {
return nil, fmt.Errorf("failed to marshal fields to JSON, %w", err) return nil, fmt.Errorf("failed to marshal fields to JSON, %v", err)
} }
return b.Bytes(), nil return b.Bytes(), nil

View File

@ -5,7 +5,7 @@ import (
"encoding/json" "encoding/json"
"testing" "testing"
"git.internal/re/logrus" "github.com/sirupsen/logrus"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )

View File

@ -9,11 +9,6 @@ import (
"time" "time"
) )
// LogFunction For big messages, it can be more efficient to pass a function
// and only call it if the log level is actually enables rather than
// generating the log message and then checking if the level is enabled
type LogFunction func() []interface{}
type Logger struct { type Logger struct {
// The logs are `io.Copy`'d to this in a mutex. It's common to set this to a // The logs are `io.Copy`'d to this in a mutex. It's common to set this to a
// file, or leave it default which is `os.Stderr`. You can also set this to // file, or leave it default which is `os.Stderr`. You can also set this to
@ -44,9 +39,6 @@ type Logger struct {
entryPool sync.Pool entryPool sync.Pool
// Function to exit the application, defaults to `os.Exit()` // Function to exit the application, defaults to `os.Exit()`
ExitFunc exitFunc ExitFunc exitFunc
// The buffer pool used to format the log. If it is nil, the default global
// buffer pool will be used.
BufferPool BufferPool
} }
type exitFunc func(int) type exitFunc func(int)
@ -78,7 +70,7 @@ func (mw *MutexWrap) Disable() {
// //
// var log = &logrus.Logger{ // var log = &logrus.Logger{
// Out: os.Stderr, // Out: os.Stderr,
// Formatter: new(logrus.TextFormatter), // Formatter: new(logrus.JSONFormatter),
// Hooks: make(logrus.LevelHooks), // Hooks: make(logrus.LevelHooks),
// Level: logrus.DebugLevel, // Level: logrus.DebugLevel,
// } // }
@ -195,9 +187,6 @@ func (logger *Logger) Panicf(format string, args ...interface{}) {
logger.Logf(PanicLevel, format, args...) logger.Logf(PanicLevel, format, args...)
} }
// Log will log a message at the level given as parameter.
// Warning: using Log at Panic or Fatal level will not respectively Panic nor Exit.
// For this behaviour Logger.Panic or Logger.Fatal should be used instead.
func (logger *Logger) Log(level Level, args ...interface{}) { func (logger *Logger) Log(level Level, args ...interface{}) {
if logger.IsLevelEnabled(level) { if logger.IsLevelEnabled(level) {
entry := logger.newEntry() entry := logger.newEntry()
@ -206,14 +195,6 @@ func (logger *Logger) Log(level Level, args ...interface{}) {
} }
} }
func (logger *Logger) LogFn(level Level, fn LogFunction) {
if logger.IsLevelEnabled(level) {
entry := logger.newEntry()
entry.Log(level, fn()...)
logger.releaseEntry(entry)
}
}
func (logger *Logger) Trace(args ...interface{}) { func (logger *Logger) Trace(args ...interface{}) {
logger.Log(TraceLevel, args...) logger.Log(TraceLevel, args...)
} }
@ -253,45 +234,6 @@ func (logger *Logger) Panic(args ...interface{}) {
logger.Log(PanicLevel, args...) logger.Log(PanicLevel, args...)
} }
func (logger *Logger) TraceFn(fn LogFunction) {
logger.LogFn(TraceLevel, fn)
}
func (logger *Logger) DebugFn(fn LogFunction) {
logger.LogFn(DebugLevel, fn)
}
func (logger *Logger) InfoFn(fn LogFunction) {
logger.LogFn(InfoLevel, fn)
}
func (logger *Logger) PrintFn(fn LogFunction) {
entry := logger.newEntry()
entry.Print(fn()...)
logger.releaseEntry(entry)
}
func (logger *Logger) WarnFn(fn LogFunction) {
logger.LogFn(WarnLevel, fn)
}
func (logger *Logger) WarningFn(fn LogFunction) {
logger.WarnFn(fn)
}
func (logger *Logger) ErrorFn(fn LogFunction) {
logger.LogFn(ErrorLevel, fn)
}
func (logger *Logger) FatalFn(fn LogFunction) {
logger.LogFn(FatalLevel, fn)
logger.Exit(1)
}
func (logger *Logger) PanicFn(fn LogFunction) {
logger.LogFn(PanicLevel, fn)
}
func (logger *Logger) Logln(level Level, args ...interface{}) { func (logger *Logger) Logln(level Level, args ...interface{}) {
if logger.IsLevelEnabled(level) { if logger.IsLevelEnabled(level) {
entry := logger.newEntry() entry := logger.newEntry()
@ -408,10 +350,3 @@ func (logger *Logger) ReplaceHooks(hooks LevelHooks) LevelHooks {
logger.mu.Unlock() logger.mu.Unlock()
return oldHooks return oldHooks
} }
// SetBufferPool sets the logger buffer pool.
func (logger *Logger) SetBufferPool(pool BufferPool) {
logger.mu.Lock()
defer logger.mu.Unlock()
logger.BufferPool = pool
}

View File

@ -25,7 +25,7 @@ func TestFieldValueError(t *testing.T) {
t.Error("unexpected error", err) t.Error("unexpected error", err)
} }
_, ok := data[FieldKeyLogrusError] _, ok := data[FieldKeyLogrusError]
require.True(t, ok, `cannot found expected "logrus_error" field: %v`, data) require.True(t, ok)
} }
func TestNoFieldValueError(t *testing.T) { func TestNoFieldValueError(t *testing.T) {
@ -67,31 +67,3 @@ func TestWarninglnNotEqualToWarning(t *testing.T) {
assert.NotEqual(t, buf.String(), bufln.String(), "Warning() and Wantingln() should not be equal") assert.NotEqual(t, buf.String(), bufln.String(), "Warning() and Wantingln() should not be equal")
} }
type testBufferPool struct {
buffers []*bytes.Buffer
get int
}
func (p *testBufferPool) Get() *bytes.Buffer {
p.get++
return new(bytes.Buffer)
}
func (p *testBufferPool) Put(buf *bytes.Buffer) {
p.buffers = append(p.buffers, buf)
}
func TestLogger_SetBufferPool(t *testing.T) {
out := &bytes.Buffer{}
l := New()
l.SetOutput(out)
pool := new(testBufferPool)
l.SetBufferPool(pool)
l.Info("test")
assert.Equal(t, pool.get, 1, "Logger.SetBufferPool(): The BufferPool.Get() must be called")
assert.Len(t, pool.buffers, 1, "Logger.SetBufferPool(): The BufferPool.Put() must be called")
}

View File

@ -15,8 +15,8 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
. "git.internal/re/logrus" . "github.com/sirupsen/logrus"
. "git.internal/re/logrus/internal/testutils" . "github.com/sirupsen/logrus/internal/testutils"
) )
// TestReportCaller verifies that when ReportCaller is set, the 'func' field // TestReportCaller verifies that when ReportCaller is set, the 'func' field
@ -40,7 +40,7 @@ func TestReportCallerWhenConfigured(t *testing.T) {
assert.Equal(t, "testWithCaller", fields["msg"]) assert.Equal(t, "testWithCaller", fields["msg"])
assert.Equal(t, "info", fields["level"]) assert.Equal(t, "info", fields["level"])
assert.Equal(t, assert.Equal(t,
"git.internal/re/logrus_test.TestReportCallerWhenConfigured.func3", fields[FieldKeyFunc]) "github.com/sirupsen/logrus_test.TestReportCallerWhenConfigured.func3", fields[FieldKeyFunc])
}) })
LogAndAssertJSON(t, func(log *Logger) { LogAndAssertJSON(t, func(log *Logger) {
@ -328,6 +328,7 @@ func TestTimeOverrideMultipleLogs(t *testing.T) {
} }
func TestDoubleLoggingDoesntPrefixPreviousFields(t *testing.T) { func TestDoubleLoggingDoesntPrefixPreviousFields(t *testing.T) {
var buffer bytes.Buffer var buffer bytes.Buffer
var fields Fields var fields Fields
@ -355,6 +356,7 @@ func TestDoubleLoggingDoesntPrefixPreviousFields(t *testing.T) {
assert.Equal(t, "omg it is!", fields["msg"]) assert.Equal(t, "omg it is!", fields["msg"])
assert.Equal(t, "eating raw fish", fields["context"]) assert.Equal(t, "eating raw fish", fields["context"])
assert.Nil(t, fields["fields.msg"], "should not have prefixed previous `msg` entry") assert.Nil(t, fields["fields.msg"], "should not have prefixed previous `msg` entry")
} }
func TestNestedLoggingReportsCorrectCaller(t *testing.T) { func TestNestedLoggingReportsCorrectCaller(t *testing.T) {
@ -377,7 +379,7 @@ func TestNestedLoggingReportsCorrectCaller(t *testing.T) {
assert.Equal(t, "looks delicious", fields["msg"]) assert.Equal(t, "looks delicious", fields["msg"])
assert.Equal(t, "eating raw fish", fields["context"]) assert.Equal(t, "eating raw fish", fields["context"])
assert.Equal(t, assert.Equal(t,
"git.internal/re/logrus_test.TestNestedLoggingReportsCorrectCaller", fields["func"]) "github.com/sirupsen/logrus_test.TestNestedLoggingReportsCorrectCaller", fields["func"])
cwd, err := os.Getwd() cwd, err := os.Getwd()
require.NoError(t, err) require.NoError(t, err)
assert.Equal(t, filepath.ToSlash(fmt.Sprintf("%s/logrus_test.go:%d", cwd, line-1)), filepath.ToSlash(fields["file"].(string))) assert.Equal(t, filepath.ToSlash(fmt.Sprintf("%s/logrus_test.go:%d", cwd, line-1)), filepath.ToSlash(fields["file"].(string)))
@ -408,7 +410,7 @@ func TestNestedLoggingReportsCorrectCaller(t *testing.T) {
assert.Equal(t, "The hardest workin' man in show business", fields["msg"]) assert.Equal(t, "The hardest workin' man in show business", fields["msg"])
assert.Nil(t, fields["fields.msg"], "should not have prefixed previous `msg` entry") assert.Nil(t, fields["fields.msg"], "should not have prefixed previous `msg` entry")
assert.Equal(t, assert.Equal(t,
"git.internal/re/logrus_test.TestNestedLoggingReportsCorrectCaller", fields["func"]) "github.com/sirupsen/logrus_test.TestNestedLoggingReportsCorrectCaller", fields["func"])
require.NoError(t, err) require.NoError(t, err)
assert.Equal(t, filepath.ToSlash(fmt.Sprintf("%s/logrus_test.go:%d", cwd, line-1)), filepath.ToSlash(fields["file"].(string))) assert.Equal(t, filepath.ToSlash(fmt.Sprintf("%s/logrus_test.go:%d", cwd, line-1)), filepath.ToSlash(fields["file"].(string)))
@ -586,48 +588,15 @@ func TestLoggingRaceWithHooksOnEntry(t *testing.T) {
logger.AddHook(hook) logger.AddHook(hook)
entry := logger.WithField("context", "clue") entry := logger.WithField("context", "clue")
var ( var wg sync.WaitGroup
wg sync.WaitGroup
mtx sync.Mutex
start bool
)
cond := sync.NewCond(&mtx)
wg.Add(100) wg.Add(100)
for i := 0; i < 50; i++ { for i := 0; i < 100; i++ {
go func() { go func() {
cond.L.Lock()
for !start {
cond.Wait()
}
cond.L.Unlock()
for j := 0; j < 100; j++ {
entry.Info("info") entry.Info("info")
}
wg.Done() wg.Done()
}() }()
} }
for i := 0; i < 50; i++ {
go func() {
cond.L.Lock()
for !start {
cond.Wait()
}
cond.L.Unlock()
for j := 0; j < 100; j++ {
entry.WithField("another field", "with some data").Info("info")
}
wg.Done()
}()
}
cond.L.Lock()
start = true
cond.L.Unlock()
cond.Broadcast()
wg.Wait() wg.Wait()
} }

View File

@ -1,4 +1,4 @@
// +build linux aix zos // +build linux aix
// +build !js // +build !js
package logrus package logrus

View File

@ -5,23 +5,30 @@ package logrus
import ( import (
"io" "io"
"os" "os"
"syscall"
"golang.org/x/sys/windows" sequences "github.com/konsorten/go-windows-terminal-sequences"
) )
func checkIfTerminal(w io.Writer) bool { func initTerminal(w io.Writer) {
switch v := w.(type) {
case *os.File:
sequences.EnableVirtualTerminalProcessing(syscall.Handle(v.Fd()), true)
}
}
func checkIfTerminal(w io.Writer) bool {
var ret bool
switch v := w.(type) { switch v := w.(type) {
case *os.File: case *os.File:
handle := windows.Handle(v.Fd())
var mode uint32 var mode uint32
if err := windows.GetConsoleMode(handle, &mode); err != nil { err := syscall.GetConsoleMode(syscall.Handle(v.Fd()), &mode)
return false ret = (err == nil)
default:
ret = false
} }
mode |= windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING if ret {
if err := windows.SetConsoleMode(handle, mode); err != nil { initTerminal(w)
return false
} }
return true return ret
}
return false
} }

View File

@ -37,11 +37,6 @@ type TextFormatter struct {
// Force quoting of all values // Force quoting of all values
ForceQuote bool ForceQuote bool
// DisableQuote disables quoting for all values.
// DisableQuote will have a lower priority than ForceQuote.
// If both of them are set to true, quote will be forced on all values.
DisableQuote bool
// Override coloring based on CLICOLOR and CLICOLOR_FORCE. - https://bixense.com/clicolors/ // Override coloring based on CLICOLOR and CLICOLOR_FORCE. - https://bixense.com/clicolors/
EnvironmentOverrideColors bool EnvironmentOverrideColors bool
@ -53,10 +48,7 @@ type TextFormatter struct {
// the time passed since beginning of execution. // the time passed since beginning of execution.
FullTimestamp bool FullTimestamp bool
// TimestampFormat to use for display when a full timestamp is printed. // TimestampFormat to use for display when a full timestamp is printed
// The format to use is the same than for time.Format or time.Parse from the standard
// library.
// The standard Library already provides a set of predefined format.
TimestampFormat string TimestampFormat string
// The fields are sorted by default for a consistent output. For applications // The fields are sorted by default for a consistent output. For applications
@ -238,8 +230,6 @@ func (f *TextFormatter) printColored(b *bytes.Buffer, entry *Entry, keys []strin
levelColor = yellow levelColor = yellow
case ErrorLevel, FatalLevel, PanicLevel: case ErrorLevel, FatalLevel, PanicLevel:
levelColor = red levelColor = red
case InfoLevel:
levelColor = blue
default: default:
levelColor = blue levelColor = blue
} }
@ -302,9 +292,6 @@ func (f *TextFormatter) needsQuoting(text string) bool {
if f.QuoteEmptyFields && len(text) == 0 { if f.QuoteEmptyFields && len(text) == 0 {
return true return true
} }
if f.DisableQuote {
return false
}
for _, ch := range text { for _, ch := range text {
if !((ch >= 'a' && ch <= 'z') || if !((ch >= 'a' && ch <= 'z') ||
(ch >= 'A' && ch <= 'Z') || (ch >= 'A' && ch <= 'Z') ||

View File

@ -59,7 +59,6 @@ func TestQuoting(t *testing.T) {
checkQuoting(false, "foo@bar") checkQuoting(false, "foo@bar")
checkQuoting(false, "foobar^") checkQuoting(false, "foobar^")
checkQuoting(false, "+/-_^@f.oobar") checkQuoting(false, "+/-_^@f.oobar")
checkQuoting(true, "foo\n\rbar")
checkQuoting(true, "foobar$") checkQuoting(true, "foobar$")
checkQuoting(true, "&foobar") checkQuoting(true, "&foobar")
checkQuoting(true, "x y") checkQuoting(true, "x y")
@ -71,30 +70,13 @@ func TestQuoting(t *testing.T) {
tf.QuoteEmptyFields = true tf.QuoteEmptyFields = true
checkQuoting(true, "") checkQuoting(true, "")
checkQuoting(false, "abcd") checkQuoting(false, "abcd")
checkQuoting(true, "foo\n\rbar")
checkQuoting(true, errors.New("invalid argument")) checkQuoting(true, errors.New("invalid argument"))
// Test forcing quotes. // Test forcing quotes.
tf.ForceQuote = true tf.ForceQuote = true
checkQuoting(true, "") checkQuoting(true, "")
checkQuoting(true, "abcd") checkQuoting(true, "abcd")
checkQuoting(true, "foo\n\rbar")
checkQuoting(true, errors.New("invalid argument")) checkQuoting(true, errors.New("invalid argument"))
// Test forcing quotes when also disabling them.
tf.DisableQuote = true
checkQuoting(true, "")
checkQuoting(true, "abcd")
checkQuoting(true, "foo\n\rbar")
checkQuoting(true, errors.New("invalid argument"))
// Test disabling quotes
tf.ForceQuote = false
tf.QuoteEmptyFields = false
checkQuoting(false, "")
checkQuoting(false, "abcd")
checkQuoting(false, "foo\n\rbar")
checkQuoting(false, errors.New("invalid argument"))
} }
func TestEscaping(t *testing.T) { func TestEscaping(t *testing.T) {

View File

@ -2,7 +2,21 @@
set -e set -e
# Install golanci 1.32.2 # Install golanci 1.21.0
if [[ "$TRAVIS_GO_VERSION" =~ ^1\.15\. ]]; then if [[ "$TRAVIS_GO_VERSION" =~ ^1\.13\. ]]; then
curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh| sh -s -- -b $(go env GOPATH)/bin v1.32.2 curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh| sh -s -- -b $(go env GOPATH)/bin v1.21.0
fi
# Only do this for go1.12 when modules are on so that it doesn't need to be done when modules are off as well.
if [[ "$TRAVIS_GO_VERSION" =~ ^1\.13\. ]] && [[ "$TRAVIS_OS_NAME" == "linux" ]] && [[ "$GO111MODULE" == "on" ]]; then
GO111MODULE=off go get github.com/dgsb/gox
fi
if [[ "$GO111MODULE" == "on" ]]; then
go mod download
fi
if [[ "$GO111MODULE" == "off" ]]; then
# Should contain all regular (not indirect) modules from go.mod
go get github.com/stretchr/testify golang.org/x/sys/unix github.com/konsorten/go-windows-terminal-sequences
fi fi

5
travis/lint.sh Executable file
View File

@ -0,0 +1,5 @@
#!/bin/bash
if [[ "$TRAVIS_GO_VERSION" =~ ^1\.13\. ]] && [[ "$TRAVIS_OS_NAME" == "linux" ]] && [[ "$GO111MODULE" == "on" ]]; then
$(go env GOPATH)/bin/golangci-lint run ./...
fi

View File

@ -4,7 +4,7 @@ import (
"log" "log"
"net/http" "net/http"
"git.internal/re/logrus" "github.com/sirupsen/logrus"
) )
func ExampleLogger_Writer_httpServer() { func ExampleLogger_Writer_httpServer() {