gtsocial-umbx

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

README.md (9952B)


      1 [![Build Status](https://travis-ci.org/dsoprea/go-logging.svg?branch=master)](https://travis-ci.org/dsoprea/go-logging)
      2 [![Coverage Status](https://coveralls.io/repos/github/dsoprea/go-logging/badge.svg?branch=master)](https://coveralls.io/github/dsoprea/go-logging?branch=master)
      3 [![Go Report Card](https://goreportcard.com/badge/github.com/dsoprea/go-logging/v2)](https://goreportcard.com/report/github.com/dsoprea/go-logging/v2)
      4 [![GoDoc](https://godoc.org/github.com/dsoprea/go-logging/v2?status.svg)](https://godoc.org/github.com/dsoprea/go-logging/v2)
      5 
      6 ## Introduction
      7 
      8 This project bridges several gaps that are present in the standard logging support in Go:
      9 
     10 - Equips errors with stacktraces and provides a facility for printing them
     11 - Inherently supports the ability for each Go file to print its messages with a prefix representing that file/package
     12 - Adds some functions to specifically log messages of different levels (e.g. debug, error)
     13 - Adds a `PanicIf()` function that can be used to conditionally manage errors depending on whether an error variable is `nil` or actually has an error
     14 - Adds support for pluggable logging adapters (so the output can be sent somewhere other than the console)
     15 - Adds configuration (such as the logging level or adapter) that can be driven from the environment
     16 - Supports filtering to show/hide the logging of certain places of the application
     17 - The loggers can be definded at the package level, so you can determine which Go file any log message came from.
     18 
     19 When used with the Panic-Defer-Recover pattern in Go, even panics rising from the Go runtime will be caught and wrapped with a stacktrace. This compartmentalizes which function they could have originated from, which is, otherwise, potentially non-trivial to figure out.
     20 
     21 ## AppEngine
     22 
     23 Go under AppEngine is very stripped down, such as there being no logging type (e.g. `Logger` in native Go) and there is no support for prefixing. As each logging call from this project takes a `Context`, this works cooperatively to bridge the additional gaps in AppEngine's logging support.
     24 
     25 With standard console logging outside of this context, that parameter will take a`nil`.
     26 
     27 
     28 ## Getting Started
     29 
     30 The simplest, possible example:
     31 
     32 ```go
     33 package thispackage
     34 
     35 import (
     36     "context"
     37     "errors"
     38 
     39     "github.com/dsoprea/go-logging/v2"
     40 )
     41 
     42 var (
     43     thisfileLog = log.NewLogger("thispackage.thisfile")
     44 )
     45 
     46 func a_cry_for_help(ctx context.Context) {
     47     err := errors.New("a big error")
     48     thisfileLog.Errorf(ctx, err, "How big is my problem: %s", "pretty big")
     49 }
     50 
     51 func init() {
     52     cla := log.NewConsoleLogAdapter()
     53     log.AddAdapter("console", cla)
     54 }
     55 ```
     56 
     57 Notice two things:
     58 
     59 1. We register the "console" adapter at the bottom. The first adapter registered will be used by default.
     60 2. We pass-in a prefix (what we refer to as a "noun") to `log.NewLogger()`. This is a simple, descriptive name that represents the subject of the file. By convention, we construct this by dot-separating the current package and the name of the file. We recommend that you define a different log for every file at the package level, but it is your choice whether you want to do this or share the same logger over the entire package, define one in each struct, etc..
     61 
     62 
     63 ### Example Output
     64 
     65 Example output from a real application (not from the above):
     66 
     67 ```
     68 2016/09/09 12:57:44 DEBUG: user: User revisiting: [test@example.com]
     69 2016/09/09 12:57:44 DEBUG: context: Session already inited: [DCRBDGRY6RMWANCSJXVLD7GULDH4NZEB6SBAQ3KSFIGA2LP45IIQ]
     70 2016/09/09 12:57:44 DEBUG: session_data: Session save not necessary: [DCRBDGRY6RMWANCSJXVLD7GULDH4NZEB6SBAQ3KSFIGA2LP45IIQ]
     71 2016/09/09 12:57:44 DEBUG: context: Got session: [DCRBDGRY6RMWANCSJXVLD7GULDH4NZEB6SBAQ3KSFIGA2LP45IIQ]
     72 2016/09/09 12:57:44 DEBUG: session_data: Found user in session.
     73 2016/09/09 12:57:44 DEBUG: cache: Cache miss: [geo.geocode.reverse:dhxp15x]
     74 ```
     75 
     76 
     77 ## Adapters
     78 
     79 This project provides one built-in logging adapter, "console", which prints to the screen. To register it:
     80 
     81 ```go
     82 cla := log.NewConsoleLogAdapter()
     83 log.AddAdapter("console", cla)
     84 ```
     85 
     86 ### Custom Adapters
     87 
     88 If you would like to implement your own logger, just create a struct type that satisfies the LogAdapter interface.
     89 
     90 ```go
     91 type LogAdapter interface {
     92     Debugf(lc *LogContext, message *string) error
     93     Infof(lc *LogContext, message *string) error
     94     Warningf(lc *LogContext, message *string) error
     95     Errorf(lc *LogContext, message *string) error
     96 }
     97 ```
     98 
     99 The *LogContext* struct passed in provides additional information that you may need in order to do what you need to do:
    100 
    101 ```go
    102 type LogContext struct {
    103     Logger *Logger
    104     Ctx context.Context
    105 }
    106 ```
    107 
    108 `Logger` represents your Logger instance.
    109 
    110 Adapter example:
    111 
    112 ```go
    113 type DummyLogAdapter struct {
    114 
    115 }
    116 
    117 func (dla *DummyLogAdapter) Debugf(lc *LogContext, message *string) error {
    118 
    119 }
    120 
    121 func (dla *DummyLogAdapter) Infof(lc *LogContext, message *string) error {
    122 
    123 }
    124 
    125 func (dla *DummyLogAdapter) Warningf(lc *LogContext, message *string) error {
    126 
    127 }
    128 
    129 func (dla *DummyLogAdapter) Errorf(lc *LogContext, message *string) error {
    130 
    131 }
    132 ```
    133 
    134 Then, register it:
    135 
    136 ```go
    137 func init() {
    138     log.AddAdapter("dummy", new(DummyLogAdapter))
    139 }
    140 ```
    141 
    142 If this is a task-specific implementation, just register it from the `init()` of the file that defines it.
    143 
    144 If this is the first adapter you've registered, it will be the default one used. Otherwise, you'll have to deliberately specify it when you are creating a logger: Instead of calling `log.NewLogger(noun string)`, call `log.NewLoggerWithAdapterName(noun string, adapterName string)`.
    145 
    146 We discuss how to configure the adapter from configuration in the "Configuration" section below.
    147 
    148 
    149 ### Adapter Notes
    150 
    151 - The `Logger` instance exports `Noun()` in the event you want to discriminate where your log entries go in your adapter. It also exports `Adapter()` for if you need to access the adapter instance from your application.
    152 - If no adapter is registered (specifically, the default adapter-name remains empty), logging calls will be a no-op. This allows libraries to implement *go-logging* where the larger application doesn't.
    153 
    154 
    155 ## Filters
    156 
    157 We support the ability to exclusively log for a specific set of nouns (we'll exclude any not specified):
    158 
    159 ```go
    160 log.AddIncludeFilter("nountoshow1")
    161 log.AddIncludeFilter("nountoshow2")
    162 ```
    163 
    164 Depending on your needs, you might just want to exclude a couple and include the rest:
    165 
    166 ```go
    167 log.AddExcludeFilter("nountohide1")
    168 log.AddExcludeFilter("nountohide2")
    169 ```
    170 
    171 We'll first hit the include-filters. If it's in there, we'll forward the log item to the adapter. If not, and there is at least one include filter in the list, we won't do anything. If the list of include filters is empty but the noun appears in the exclude list, we won't do anything.
    172 
    173 It is a good convention to exclude the nouns of any library you are writing whose logging you do not want to generally be aware of unless you are debugging. You might call `AddExcludeFilter()` from the `init()` function at the bottom of those files unless there is some configuration variable, such as "(LibraryNameHere)DoShowLogging", that has been defined and set to TRUE.
    174 
    175 
    176 ## Configuration
    177 
    178 The following configuration items are available:
    179 
    180 - *Format*: The default format used to build the message that gets sent to the adapter. It is assumed that the adapter already prefixes the message with time and log-level (since the default AppEngine logger does). The default value is: `{{.Noun}}: [{{.Level}}] {{if eq .ExcludeBypass true}} [BYPASS]{{end}} {{.Message}}`. The available tokens are "Level", "Noun", "ExcludeBypass", and "Message".
    181 - *DefaultAdapterName*: The default name of the adapter to use when NewLogger() is called (if this isn't defined then the name of the first registered adapter will be used).
    182 - *LevelName*: The priority-level of messages permitted to be logged (all others will be discarded). By default, it is "info". Other levels are: "debug", "warning", "error", "critical"
    183 - *IncludeNouns*: Comma-separated list of nouns to log for. All others will be ignored.
    184 - *ExcludeNouns*: Comma-separated list on nouns to exclude from logging.
    185 - *ExcludeBypassLevelName*: The log-level at which we will show logging for nouns that have been excluded. Allows you to hide excessive, unimportant logging for nouns but to still see their warnings, errors, etc...
    186 
    187 
    188 ### Configuration Providers
    189 
    190 You provide the configuration by setting a configuration-provider. Configuration providers must satisfy the `ConfigurationProvider` interface. The following are provided with the project:
    191 
    192 - `EnvironmentConfigurationProvider`: Read values from the environment.
    193 - `StaticConfigurationProvider`: Set values directly on the struct.
    194 
    195 **The configuration provider must be applied before doing any logging (otherwise it will have no effect).**
    196 
    197 Environments such as AppEngine work best with `EnvironmentConfigurationProvider` as this is generally how configuration is exposed *by* AppEngine *to* the application. You can define this configuration directly in *that* configuration.
    198 
    199 By default, no configuration-provider is applied, the level is defaulted to INFO and the format is defaulted to "{{.Noun}}:{{if eq .ExcludeBypass true}} [BYPASS]{{end}} {{.Message}}".
    200 
    201 Again, if a configuration-provider does not provide a log-level or format, they will be defaulted (or left alone, if already set). If it does not provide an adapter-name, the adapter-name of the first registered adapter will be used.
    202 
    203 Usage instructions of both follow.
    204 
    205 
    206 ### Environment-Based Configuration
    207 
    208 ```go
    209 ecp := log.NewEnvironmentConfigurationProvider()
    210 log.LoadConfiguration(ecp)
    211 ```
    212 
    213 Each of the items listed at the top of the "Configuration" section can be specified in the environment using a prefix of "Log" (e.g. LogDefaultAdapterName).
    214 
    215 
    216 ### Static Configuration
    217 
    218 ```go
    219 scp := log.NewStaticConfigurationProvider()
    220 scp.SetLevelName(log.LevelNameWarning)
    221 
    222 log.LoadConfiguration(scp)
    223 ```