~mendelmaleh/log

2d9e3a3484441a201c609f22415f84449f977163 — Mendel E 10 months ago e5a8be6
Move stdlog to top level package

Add funcs so that the logger can be used w/o setup, like the standard
log package.
2 files changed, 63 insertions(+), 23 deletions(-)

D handlers/stdlog.go
A std.go
D handlers/stdlog.go => handlers/stdlog.go +0 -23
@@ 1,23 0,0 @@
package handlers

import (
	"os"
	"strings"

	"git.sr.ht/~mendelmaleh/log"
)

// StdLog tries to imitate the standard log package
type StdLog struct{}

// Write implements the log.Handler interface
func (*StdLog) Write(l *log.Log) {
	time := l.Time.Format("2006/01/02 15:04:05")
	b := []byte(strings.Join([]string{time, l.Text}, " "))

	if b[len(b)-1] != '\n' {
		b = append(b, '\n')
	}

	os.Stderr.Write(b)
}

A std.go => std.go +63 -0
@@ 0,0 1,63 @@
package log

import (
	"fmt"
	"os"
	"strings"
	"time"
)

// StdLog tries to imitate the standard log package
type StdLog struct{}

// Write implements the log.Handler interface
func (*StdLog) Write(l *Log) {
	time := l.Time.Format("2006/01/02 15:04:05")
	b := []byte(strings.Join([]string{time, l.Text}, " "))

	if b[len(b)-1] != '\n' {
		b = append(b, '\n')
	}

	os.Stderr.Write(b)
}

var std = New(&StdLog{})

// Print outputs a log with Info level.
// Arguments are handled in the manner of fmt.Print.
func Print(v ...interface{}) {
	std.Output(&Log{Level: LInfo, Time: time.Now(), Text: fmt.Sprint(v...)})
}

// Printf outputs a log with Info level.
// Arguments are handled in the manner of fmt.Printf.
func Printf(f string, v ...interface{}) {
	std.Output(&Log{Level: LInfo, Time: time.Now(), Text: fmt.Sprintf(f, v...)})
}

// Fatal outputs a log with Fatal level followed by os.Exit(1).
func Fatal(v ...interface{}) {
	std.Output(&Log{Level: LInfo, Time: time.Now(), Text: fmt.Sprint(v...)})
	os.Exit(1)
}

// Fatalf outputs a log with Fatal level followed by os.Exit(1).
func Fatalf(f string, v ...interface{}) {
	std.Output(&Log{Level: LFatal, Time: time.Now(), Text: fmt.Sprintf(f, v...)})
	os.Exit(1)
}

// Panic outputs a log with Panic level followed by panic().
func Panic(v ...interface{}) {
	t, s := time.Now(), fmt.Sprint(v...)
	std.Output(&Log{Level: LPanic, Time: t, Text: s})
	panic(s)
}

// Panicf outputs a log with Panic level followed by panic().
func Panicf(f string, v ...interface{}) {
	t, s := time.Now(), fmt.Sprintf(f, v...)
	std.Output(&Log{Level: LPanic, Time: t, Text: s})
	panic(s)
}