https://github.com/mitinarseny/gol
A small wrapper for standard log package in Go with enhanced prefixes.
https://github.com/mitinarseny/gol
logging
Last synced: 11 months ago
JSON representation
A small wrapper for standard log package in Go with enhanced prefixes.
- Host: GitHub
- URL: https://github.com/mitinarseny/gol
- Owner: mitinarseny
- Created: 2019-06-14T10:42:26.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2019-06-30T15:55:59.000Z (almost 7 years ago)
- Last Synced: 2025-02-22T13:58:52.328Z (over 1 year ago)
- Topics: logging
- Language: Go
- Homepage:
- Size: 28.3 KB
- Stars: 1
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# gol [](https://travis-ci.org/mitinarseny/gol) [](https://codecov.io/gh/mitinarseny/gol) [](https://golangci.com/r/github.com/mitinarseny/gol) [](https://godoc.org/github.com/mitinarseny/gol)
A small wrapper for standard [log](https://golang.org/pkg/log/) package in Go with enhanced prefixes.
## Install
```bash
go get github.com/mitinarseny/gol
```
## Documentation
See documentation for this package on [GoDoc](https://godoc.org/github.com/mitinarseny/gol).
## Usage
Create `gol.Logger` from `log.Logger`:
```go
l := gol.New(log.New(os.Stdout, "", 0))
```
Type `gol.Logger` has `*log.Logger` embedded in it, so you can use all functions from [log](https://golang.org/pkg/log/) package.
You can use following functions to change prefix of logger:
* `l.SetPersistentPrefix("prefix")`
* `l.SetPersistentPrefixf("%s pattern")`
* `l.SetPrefix("prefix")`
* `l.SetPrefixf("%s pattern")`
All these functions return `RestoreFunc` which can be used to restore previous prefix.
It is recommended to use it with `defer` keyword:
```go
func someFunc() {
defer l.SetPrefixf(" %s")()
...
}
```
## Example
Consider an example below:
```go
// main.go
import (
"os"
"log"
"github.com/mitinarseny/gol"
)
func main() {
l := gol.New(log.New(os.Stdout, "", 0))
r1 := l.SetPersistentPrefix("gol ")
l.Println("is logger for Go with enhanced prefixes")
r2 := l.SetPrefix("is cool because ")
l.Println("you can grow prefixes easily")
r3 := l.SetPrefixf("%syou ")
l.Println("do not have to repeat yourself")
r3()
l.Println("it is easy to restore previous prefixes")
r2()
l.Println("is very cool")
r1()
l.Println("that's it :)")
}
```
This code will produce following output:
```
gol is logger for Go with enhanced prefixes
gol is cool because you can grow prefixes easily
gol is cool because you do not have to repeat yourself
gol is cool because a lot of reasnons
gol is very cool
that's it :)
```