Logging to file.
This commit is contained in:
parent
57a33bcbcc
commit
27819dbf0e
|
@ -0,0 +1,71 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/kisom/goutils/logging"
|
||||||
|
"github.com/kisom/testio"
|
||||||
|
)
|
||||||
|
|
||||||
|
var log = logging.Init()
|
||||||
|
var olog = logging.New("subsystem #42", logging.LevelNotice)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
exampleNewWriters()
|
||||||
|
log.Notice("Hello, world.")
|
||||||
|
log.Warning("this program is about to end")
|
||||||
|
|
||||||
|
olog.Print("now online")
|
||||||
|
logging.Suppress("olog")
|
||||||
|
olog.Print("extraneous information")
|
||||||
|
|
||||||
|
logging.Enable("olog")
|
||||||
|
olog.Print("relevant now")
|
||||||
|
|
||||||
|
logging.SuppressAll()
|
||||||
|
log.Alert("screaming into the void")
|
||||||
|
olog.Critical("can anyone hear me?")
|
||||||
|
|
||||||
|
log.Enable()
|
||||||
|
log.Notice("i'm baaack")
|
||||||
|
log.Suppress()
|
||||||
|
log.Warning("but not for long")
|
||||||
|
|
||||||
|
logging.EnableAll()
|
||||||
|
log.Notice("fare thee well")
|
||||||
|
olog.Print("all good journeys must come to an end")
|
||||||
|
exampleNewFromFile()
|
||||||
|
}
|
||||||
|
|
||||||
|
func exampleNewWriters() {
|
||||||
|
o := testio.NewBufCloser(nil)
|
||||||
|
e := testio.NewBufCloser(nil)
|
||||||
|
|
||||||
|
wlog := logging.NewFromWriters("writers", logging.DefaultLevel, o, e)
|
||||||
|
wlog.Notice("hello, world")
|
||||||
|
wlog.Notice("some more things happening")
|
||||||
|
wlog.Warning("something suspicious has happened")
|
||||||
|
wlog.Alert("pick up that can, Citizen!")
|
||||||
|
|
||||||
|
fmt.Println("--- BEGIN OUT ---")
|
||||||
|
fmt.Printf("%s", o.Bytes())
|
||||||
|
fmt.Println("--- END OUT ---")
|
||||||
|
|
||||||
|
fmt.Println("--- BEGIN ERR ---")
|
||||||
|
fmt.Printf("%s", e.Bytes())
|
||||||
|
fmt.Println("--- END ERR ---")
|
||||||
|
}
|
||||||
|
|
||||||
|
func exampleNewFromFile() {
|
||||||
|
flog, err := logging.NewFromFile("file logger", logging.LevelNotice,
|
||||||
|
"example.log", "example.err", true)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("failed to open logger: %v", err)
|
||||||
|
}
|
||||||
|
defer flog.Close()
|
||||||
|
|
||||||
|
flog.Notice("hello, world")
|
||||||
|
flog.Notice("some more things happening")
|
||||||
|
flog.Warning("something suspicious has happened")
|
||||||
|
flog.Alert("pick up that can, Citizen!")
|
||||||
|
}
|
|
@ -124,20 +124,22 @@ func NewFromFile(domain string, level Level, outFile, errFile string, multiplex
|
||||||
level: level,
|
level: level,
|
||||||
}
|
}
|
||||||
|
|
||||||
var err error
|
outf, err := os.OpenFile(outFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
||||||
l.out, err = os.OpenFile(outFile, os.O_APPEND|os.O_CREATE, 0644)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
l.err, err = os.OpenFile(errFile, os.O_APPEND|os.O_CREATE, 0644)
|
errf, err := os.OpenFile(errFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if multiplex {
|
if multiplex {
|
||||||
l.out = mwc.MultiWriteCloser(l.out, os.Stdout)
|
l.out = mwc.MultiWriteCloser(outf, os.Stdout)
|
||||||
l.err = mwc.MultiWriteCloser(l.err, os.Stderr)
|
l.err = mwc.MultiWriteCloser(errf, os.Stderr)
|
||||||
|
} else {
|
||||||
|
l.out = outf
|
||||||
|
l.err = errf
|
||||||
}
|
}
|
||||||
|
|
||||||
Enable(domain)
|
Enable(domain)
|
||||||
|
|
Loading…
Reference in New Issue