An open API service providing package, version and dependency metadata of many open source software ecosystems and registries.

Top 8.2% on proxy.golang.org

proxy.golang.org : github.com/rhizomplatform/log

Package log provides an easy way to register application logs from any other part of the application. In the default configuration, the logging happens both on the stdout and on disk (with log rotation already included). To set up the global logger, use the Setup function. This function sets up both the file logging and the stdout logging, and let the logging facilities available through the functions Debug, Info, Warn, and Error. Once the setup is done, subsequent calls to Setup will be ignored. To dispose of the current logger, use the TearDown function. The functions Debug, Info and Warn all accept only a string as parameter, and that is fine as long as the message to be logged is simple: As soon as you need to add more dynamic information to the log entries, you might be tempted to do something like that: While this works on the surface, this approach is problematic because an important piece of information (in this example, the server port) is now 'buried' into a formatted string and logging tools cannot easily identify this. To better handle this situation, use the With function to add custom fields as extra logging information: Note how the relevant information is now more easily accessible, both for human and machine consumption. This is what is called 'structured logging': giving log messages clearer and easy to parse contextual information. The With function generates a log entry (i.e. it does not print anything) with one or more custom fields, where the keys are strings and the values can be any arbitrary type. The log.F type is just a handy shortcut to a map[string]interface{}. Since log entries are not printed immediately, you need to eventually 'finalize' and entry (e.g. calling Info, Warn, etc.) to make it show. On the other hand, the option to construct a log entry in small steps can be handy in some situations: While this example is anecdotal, it is important to know that the possibility of exposing more (or less) information on a log entry exists. While most of the log functions and entry methods accept a string as argument, the function Error accepts an error instance instead. This function extracts the error message and the stack trace of the error (more on stack trace extraction below) and prints the relevant log entry: If you want to add a custom message or custom fields to and error entry, use a combination of With and WithError calls: Note that once an error is added to an entry (using WithError) the entry itself locked into an 'error state', that introduces two big changes: (1) it is not possible to use the non-error methods to finalize the entry, and (2) the regular Error(error) method signature is changed to Error(string), to easily accommodate a complementary error message. In other words: First things first: the stack trace is only visible in the log files, never on screen output. When a log entry in the error level is created, the entry records not only the error message, but the existing stack trace too. If using the pkg/errors package (https://github.com/pkg/errors), the stack trace is embedded in the error instance at the creation. However, when using the error package of the stdlib, no stack trace is created. In these cases, a brand new stack trace is created from the point the error is logged. While useful, this may not point to the exact root cause of the problem: In the example above example, the stack trace will register the 'origin' of the error in myFunc as oposed to the real location of the problem, errorFunc. This is expected, because since the stdlib does not generate a trace, the logging library generates one from scratch, in the point that the log happens. If the 'correct' place of the error is crucial, consider using the pkg/errors constructor to create the error. Note that while the logger recognizes the pkg/errors generated stack, it DOES NOT recognize the error Cause or other library-specific information.

Registry - Source - Documentation - JSON
purl: pkg:golang/github.com/rhizomplatform/log
License: MIT
Latest release: 9 months ago
First release: over 5 years ago
Namespace: github.com/rhizomplatform
Stars: 1 on GitHub
Forks: 0 on GitHub
See more repository details: repos.ecosyste.ms
Last synced: 9 days ago

    Loading...
    Readme
    Loading...