https://github.com/txchen/tlog
a simple log library writes to both file and console
https://github.com/txchen/tlog
Last synced: 9 months ago
JSON representation
a simple log library writes to both file and console
- Host: GitHub
- URL: https://github.com/txchen/tlog
- Owner: txchen
- License: mit
- Created: 2015-02-02T23:41:06.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2015-05-25T16:34:40.000Z (about 11 years ago)
- Last Synced: 2025-10-11T13:16:49.428Z (9 months ago)
- Language: Go
- Homepage:
- Size: 176 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# tlog [](https://app.wercker.com/project/bykey/5cf56d48878565a6da12cafd85e1ac8c)
Package tlog provides simple APIs to write log to console and file simultaneously.
Tlog is a wrapper around golang's standard [log](http://golang.org/pkg/log) package.
Features:
* Predefined log levels, from debug to critical.
* Console and logfile loglevel can be set separately.
* Console log writes to stderr or stdout according to log level.
Log levels:
* DEBUG - 1
* INFO - 2
* WARN - 3
* ERROR - 4
* CRITICAL - 5
* OFF - 6
### Usage
```go
package main
import (
"log"
"github.com/txchen/tlog"
)
func main() {
// By default, only log to console
tlog.WARN.Println("warn")
// Default console loglevel is WARN, we can change it
tlog.SetConsoleLogLevel(tlog.LevelInfo)
tlog.INFO.Println("now I can show")
// Set logfile to enable file based logging
tlog.SetLogFile("z.log")
tlog.CRITICAL.Printf("2 + 2 = %d", 4)
// By default, logfile loglevel is INFO, let's change it
tlog.SetLogfileLogLevel(tlog.LevelDebug)
tlog.Debug.Println("you can only see me in file")
// By default, tlog use Ldate | Ltime flag, you can tune it
tlog.SetLogFlags(log.Ldate | log.Ltime | log.Lshortfile)
// Now you can see different logging style
tlog.ERROR.Println("different style")
// And you can turn off the flag
tlog.SetLogFlags(0)
// Now the output will be "WARN: clean"
tlog.WARN.Println("clean")
// To turn off logging, set the level to off
tlog.SetLogfileLogLevel(tlog.LevelOff)
tlog.SetConsoleLogLevel(tlog.LevelOff)
tlog.CRITICAL.Println("no one can see me")
}
```