github.com/amazechain/amc@v0.1.3/log/logrus-prefixed-formatter/README.md (about)

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