Giter VIP home page Giter VIP logo

logrus-prefixed-formatter's Introduction

Logrus Prefixed Log Formatter

Build Status

Logrus formatter mainly based on original logrus.TextFormatter but with slightly modified colored output and support for log entry prefixes, e.g. message source followed by a colon. In addition, custom color themes are supported.

Formatter screenshot

Just like with the original logrus.TextFormatter when a TTY is not attached, the output is compatible with the logfmt format:

time="Oct 27 00:44:26" level=debug msg="Started observing beach" animal=walrus number=8
time="Oct 27 00:44:26" level=info msg="A group of walrus emerges from the ocean" animal=walrus size=10
time="Oct 27 00:44:26" level=warning msg="The group's number increased tremendously!" number=122 omg=true
time="Oct 27 00:44:26" level=debug msg="Temperature changes" temperature=-4
time="Oct 27 00:44:26" level=panic msg="It's over 9000!" animal=orca size=9009
time="Oct 27 00:44:26" level=fatal msg="The ice breaks!" number=100 omg=true
exit status 1

Installation

To install formatter, use go get:

$ go get github.com/x-cray/logrus-prefixed-formatter

Usage

Here is how it should be used:

package main

import (
	"github.com/sirupsen/logrus"
	prefixed "github.com/x-cray/logrus-prefixed-formatter"
)

var log = logrus.New()

func init() {
	log.Formatter = new(prefixed.TextFormatter)
	log.Level = logrus.DebugLevel
}

func main() {
	log.WithFields(logrus.Fields{
		"prefix": "main",
		"animal": "walrus",
		"number": 8,
	}).Debug("Started observing beach")

	log.WithFields(logrus.Fields{
		"prefix":      "sensor",
		"temperature": -4,
	}).Info("Temperature changes")
}

API

prefixed.TextFormatter exposes the following fields and methods.

Fields

  • ForceColors bool — set to true to bypass checking for a TTY before outputting colors.
  • DisableColors bool — force disabling colors. For a TTY colors are enabled by default.
  • DisableUppercase bool — set to true to turn off the conversion of the log level names to uppercase.
  • ForceFormatting bool — force formatted layout, even for non-TTY output.
  • DisableTimestamp bool — disable timestamp logging. Useful when output is redirected to logging system that already adds timestamps.
  • FullTimestamp bool — enable logging the full timestamp when a TTY is attached instead of just the time passed since beginning of execution.
  • TimestampFormat string — timestamp format to use for display when a full timestamp is printed.
  • DisableSorting bool — the fields are sorted by default for a consistent output. For applications that log extremely frequently and don't use the JSON formatter this may not be desired.
  • QuoteEmptyFields bool — wrap empty fields in quotes if true.
  • QuoteCharacter string — can be set to the override the default quoting character " with something else. For example: ', or `.
  • SpacePadding int — pad msg field with spaces on the right for display. The value for this parameter will be the size of padding. Its default value is zero, which means no padding will be applied.

Methods

SetColorScheme(colorScheme *prefixed.ColorScheme)

Sets an alternative color scheme for colored output. prefixed.ColorScheme struct supports the following fields:

  • InfoLevelStyle string — info level style.
  • WarnLevelStyle string — warn level style.
  • ErrorLevelStyle string — error style.
  • FatalLevelStyle string — fatal level style.
  • PanicLevelStyle string — panic level style.
  • DebugLevelStyle string — debug level style.
  • PrefixStyle string — prefix style.
  • TimestampStyle string — timestamp style.

Color styles should be specified using mgutz/ansi style syntax. For example, here is the default theme:

InfoLevelStyle:  "green",
WarnLevelStyle:  "yellow",
ErrorLevelStyle: "red",
FatalLevelStyle: "red",
PanicLevelStyle: "red",
DebugLevelStyle: "blue",
PrefixStyle:     "cyan",
TimestampStyle:  "black+h"

It's not necessary to specify all colors when changing color scheme if you want to change just specific ones:

formatter.SetColorScheme(&prefixed.ColorScheme{
    PrefixStyle:    "blue+b",
    TimestampStyle: "white+h",
})

License

MIT

logrus-prefixed-formatter's People

Contributors

jhaynie avatar kisonecat avatar mbyczkowski avatar niyue avatar thechriswalker avatar x-cray avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

logrus-prefixed-formatter's Issues

Option to set/force isTerminal to be true.

I've wrapped the standard os.Stdout/os.Stderr io.Writer options, and also use io.MultiWriter for potentially multiple output targets. I do this because I have a layer of text processing to take place before output hits the terminal. With the latest updates to this library (and logrus) logging is now downgraded because io.Writer is not an *os.File but instead my custom wrapping type. Perhaps this is really an issue with logrus but it seems like it would be nice to force terminal status.

`QuoteEmptyFields` attribute does not work

QuoteEmptyFields does not quote empty fields even when set to true explicitly. The following code

package main

import (
	"time"

	"github.com/sirupsen/logrus"
	prefixed "github.com/x-cray/logrus-prefixed-formatter"
)

func init() {
	logrus.SetFormatter(&prefixed.TextFormatter{
		TimestampFormat:  time.RFC3339,
		FullTimestamp:    true,
		ForceFormatting:  true,
		QuoteEmptyFields: true,
	})
	logrus.SetLevel(logrus.DebugLevel)

}

func main() {
	logrus.WithFields(logrus.Fields{
		"prefix":      "main",
		"emptyfield1": "",
		"emptyfield2": "",
		"number":      0,
	}).Debug("Started observing beach")
}

outputs

[2023-01-28T18:32:38+05:00] DEBUG main: Started observing beach emptyfield1= emptyfield2= number=0

Notice the empty fields are not quoted. Am I missing something here?

Log file and line

Is therere any possibility to log file and line like

Info = log.New(infoHandle,
        "INFO: ",
        log.Ldate|log.Ltime|log.Lshortfile)

In standard package log ?

Each log entry is appended to same line

I'm using this formatter in conjunction with https://github.com/rifflock/lfshook to save the log to a file, like this:

logger.Hooks.Add(lfshook.NewHook( LOG_FILE_PATH, &prefixed.TextFormatter{ FullTimestamp: true, TimestampFormat: "2006-01-02T15:04:05.999Z", DisableColors: runtime.GOOS == "windows", }, ))

However, when writing to the file the next log entry is appended on the same line.
Is there an option in the formatter to write it on a newline, like happens in the terminal?

EDIT: My bad, seems to be fine in a more advanced editor than Notepad

No tty disabled colored output

Love the library. I may implement the colorized output formatter with the colors disabled without a tty for a project of mine . Is this a feature you have considered or planned?

Undefined: logrus.DefaultTimestampFormat

Hi,

I recently updated the dependencies of a project and got this error: .go/src/github.com/x-cray/logrus-prefixed-formatter/formatter.go:178: undefined: logrus.DefaultTimestampFormat

I believe it has to do with this commit.

Just a heads up!

Thanks!

Position of msg field

In logrus.TextFormatter, the msg field is always the third field (after time and level).

This makes sense because the other tags are probably additional information about the error, compare:

time="2017-06-28T13:37:18+02:00" level=fatal msg="Can't open file" filename="/usr/local/share/man" 

vs

time="2017-06-28T13:37:18+02:00" level=fatal filename="/usr/local/share/man" msg="Can't open file"

In prefixed.TextFormatter it is always at the end, which in my opinion makes the log more difficult to read.

Add a way to change colours

Hi!

I really like what your project.
Could you put the colours your format use in some struct/fields ?

On my terminal, with solarized colour scheme, I can't see the timestamp or the message...

Maybe putting the colours in a struct, allow the user to create easily colour themes...

Multiline input gets quoted, with line feed characters escaped

Hi!

I have a multiline input to log (the content of a short file), and without your formatter,
it is written as I expect to.

But your formatter escapes the new line (\n) if I don't use colors.
It is the needsQuote part that does that I think...

Can you do anything for me?

panic due to concurrent map read / write

This happened a few times in a server under intense load (with lots of goroutines).

fatal error: concurrent map read and map write
time="2018-01-17T22:40:01.546780408Z" level=info msg="sending slack message" prefix="tools/prober" source="main.sendSlackMessage([...]/tools/prober/slack.go:124)"

goroutine 276445 [running]:
runtime.throw(0xb28baa, 0x21)
        /usr/lib/google-golang/src/runtime/panic.go:608 +0x9e fp=0xc4340ab5b8 sp=0xc4340ab598 pc=0x542cce
runtime.mapaccess1_faststr(0xa5e5c0, 0xc4201d1350, 0xb1659c, 0x6, 0xa1d0e0)
        /usr/lib/google-golang/src/runtime/hashmap_fast.go:217 +0x43a fp=0xc4340ab610 sp=0xc4340ab5b8 pc=0x522f7a
[...]/vendor/github.com/x-cray/logrus-prefixed-formatter.(*TextFormatter).Format(0xc420080aa0, 0xc42c929cc0, 0xc42014a400, 0xafeb60, 0xc421e280e0, 0x0, 0xc42def1740)
        /[...]/vendor/github.com/x-cray/logrus-prefixed-formatter/formatter.go:216 +0x4a3 fp=0xc4340ab7a0 sp=0xc4340ab610 pc=0x939a83
[...]/vendor/github.com/sirupsen/logrus.Entry.log(0xc420080460, 0xc4201d1350, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xc400000004, ...)
        /[...]/vendor/github.com/sirupsen/logrus/entry.go:109 +0x22c fp=0xc4340ab870 sp=0xc4340ab7a0 pc=0x902c4c
[...]/vendor/github.com/sirupsen/logrus.(*Entry).Info(0xc4201d2730, 0xc4340ab948, 0x1, 0x1)
        /[...]/vendor/github.com/sirupsen/logrus/entry.go:144 +0xac fp=0xc4340ab8e0 sp=0xc4340ab870 pc=0x90322c
main.sendSlackMessage(0xc428cd56b0, 0xc, 0xb2192a)
        /[...]/tools/prober/slack.go:124 +0x7d fp=0xc4340ab968 sp=0xc4340ab8e0 pc=0x9afe8d
main.(*environment).slackAlerts(0xc420199490)
        /[...]/tools/prober/slack.go:70 +0x4e3 fp=0xc4340abaf0 sp=0xc4340ab968 pc=0x9af333
main.(*environment).check(0xc420199490)
        /[...]/tools/prober/prober.go:325 +0x13fb fp=0xc4340abfa0 sp=0xc4340abaf0 pc=0x9ae45b
main.(*environment).start.func1(0xc4201ee410, 0xc420199490)
        /[...]/tools/prober/prober.go:344 +0x5a fp=0xc4340abfd0 sp=0xc4340abfa0 pc=0x9b030a
runtime.goexit()
        /usr/lib/google-golang/src/runtime/asm_amd64.s:2337 +0x1 fp=0xc4340abfd8 sp=0xc4340abfd0 pc=0x5735d1
created by main.(*environment).start
        /[...]/tools/prober/prober.go:342 +0x9f

Multiline logs entry stays multiline when it is written to a file.

Hi,

It used to work because I referenced it in another issue #9 (which was not one).

But it does not work anymore:

time="2017-06-12T17:36:43+02:00" level=error msg="Command failed" ... Msg="raidcom: [EX_CMDRJE] An order to the control/command device was rejected
It was rejected due to SKEY=0x05, ASC=0x26, ASCQ=0x00, SSB=0xB958,0x0233 on Serial#(358702)
CAUSE : It cannot be deleted because the operation object LU is executing host I/O.
" Nature=Locked RequestID=2eebe597-e33a-55be-bd78-35d7ef6db86f Status=221 Subcommand=delete ....

I noticed that you use %v method for quoting, but as shown here that does not work as intended.

You may use fmt.Fprintf(b, "%q", value) instead but you'll need to strip the first and final character to remove the quotes.

Do you want me to do a pull request of any kind?

Please create a tag for changes from PR #4

Since there was a breaking change in logrus, can you please provide two tags? One before the change of #4 which points out that logrus <= 0.11.0 is supported and one that says that it is compatible with logrus >= 0.11.1 ? That would make live way easier for people like me, that are using tools like glide, because i do not have to search for commit hashes then ;)

Thanks

logrus removed IsTerminal()

Hi,

First ever issue, forgive any noobness please.

Logrus has made breaking changes (without bumping semantic version!). The logrus.IsTerminal() function has been removed and this breaks prefixed-formatter.

The logrus change can be seen here and go get fails for logrus-prefixed-formatter:

go get -u github.com/x-cray/logrus-prefixed-formatter
# github.com/x-cray/logrus-prefixed-formatter
../../../../../github.com/x-cray/logrus-prefixed-formatter/formatter.go:147: undefined: logrus.IsTerminal

The problem for prefixed-formatter is in formatter.go:147

A quick solution is to change formatter.go:147 to

f.isTerminal = f.checkIfTerminal(entry.Logger.Out)

and copy the private function checkIfTerminal() from logrus text_formatter.go:72 into formatter.go.

func (f *TextFormatter) checkIfTerminal(w io.Writer) bool {
	switch v := w.(type) {
	case *os.File:
		return terminal.IsTerminal(int(v.Fd()))
	default:
		return false
	}
}

This introduces a dependency on golang.org/x/crypto/ssh/terminal

With these changes the test suite runs correctly as do my applications. I don't know enough about the guts of logrus and terminals to put a 100% guarantee on the soundness of this solution.

If there's anything else I can do, let me know.

Create custom formmater

Hi,

I've created this question in stack overflow.

https://stackoverflow.com/questions/48971780/go-change-format-of-log-output-logrus
what I need is formatting like
[INFO]:2018-02-25 10:42:20 - Starting Process
There is a simple way to do it with your lib ?

I use go logrus and I need to change the format of the output message, I try several of things without success

what I need is format like this with formatter of logrus

[error]: datetime - message

What I've tried without success is this

logger := &logrus.Logger{
Out: os.Stderr,
Level: level,
Formatter: &logrus.TextFormatter{
DisableColors: true,
TimestampFormat : "2006-01-02 15:04:05",
FullTimestamp:true,

    },
}

This produce the following

time="2018-02-25 10:42:20" level=error msg="Starting Process"

And I want it to be

[INFO]:2018-02-25 10:42:20 - Starting Process

not work on cmd

�[90m[0000]�[0m �[34mDEBUG�[0m�[36m main:�[0m Started observing beach �[34manimal�[0m=walrus �[34mnumber�[0m=8
�[90m[0000]�[0m �[32m INFO�[0m�[36m sensor:�[0m Temperature changes �[32mtemperature�[0m=-4

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.