Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/uniplaces/go-logger
https://github.com/uniplaces/go-logger
Last synced: 25 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/uniplaces/go-logger
- Owner: uniplaces
- Created: 2017-06-06T14:54:39.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2020-01-10T12:20:45.000Z (almost 5 years ago)
- Last Synced: 2024-12-01T07:40:01.994Z (27 days ago)
- Language: Go
- Size: 626 KB
- Stars: 3
- Watchers: 24
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# go-logger
Structured logger for Uniplaces Go projects.
## Usage
#### Import
```go
import logger "github.com/uniplaces/go-logger"
```#### Setup
Initializing the logger requires a config as parameter where you can
set which is the current environment and the desired log level.```go
logger.Init(logger.NewConfig("staging", "warning"))
```Note: make sure you call `Init` only once and as early as possible in your program.
#### Use
Since the logger is implemented as a singleton, you can access it anywhere.##### Simple use
```go
logger.Debug("debug message")
logger.Error(errors.New("error message"))
```##### Structured use
```go
logger.
Builder().
AddField("test", "value").
AddContextField("foo", "bar").
Debug("debug message")
```##### Adding default fields
```go
// Normal field
logger.AddDefaultField("normal-field", "normal_field_value", false)// Context field
logger.AddDefaultField("context-field", "context_field_value", true)
```## Guidelines
This project's main focus is to bring a common standard to our Go projects's logs and, as such,
please try to follow these guidelines when using this logger:#### 1 - Add contextual information when logging an error
###### Use `AddContextField(key string, value interface{})`.
Example: when trying to fetch something from dynamodb and it fails.
In this case, contextual logging would include the ID of the object you're trying to fetch.
_**Note:** avoid using this when handling sensitive information!_
---
#### 2 - Log as soon as possible
Logs should be specific and easy to trace back, so generic error logging should be avoided.