mirror of
https://github.com/taigrr/log-socket
synced 2025-01-18 04:53:14 -08:00
minor cleanup, adds support for formatted prints
This commit is contained in:
parent
5f18def7fa
commit
a07d3a11df
115
logger/logger.go
115
logger/logger.go
@ -10,16 +10,6 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
|
||||||
LTrace Level = iota
|
|
||||||
LDebug
|
|
||||||
LInfo
|
|
||||||
LWarn
|
|
||||||
LError
|
|
||||||
LPanic
|
|
||||||
LFatal
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
var (
|
||||||
clients []*Client
|
clients []*Client
|
||||||
sliceTex sync.Mutex
|
sliceTex sync.Mutex
|
||||||
@ -142,9 +132,19 @@ func Trace(args ...interface{}) {
|
|||||||
level: LTrace,
|
level: LTrace,
|
||||||
}
|
}
|
||||||
createLog(e)
|
createLog(e)
|
||||||
// entry := logger.WithFields(logrus.Fields{})
|
}
|
||||||
// entry.Data["file"] = fileInfo(2)
|
|
||||||
// entry.Debug(args...)
|
// Formatted print for Trace
|
||||||
|
func Tracef(format string, args ...interface{}) {
|
||||||
|
output := fmt.Sprintf(format, args...)
|
||||||
|
e := Entry{
|
||||||
|
Timestamp: time.Now(),
|
||||||
|
Output: output,
|
||||||
|
File: fileInfo(2),
|
||||||
|
Level: "TRACE",
|
||||||
|
level: LTrace,
|
||||||
|
}
|
||||||
|
createLog(e)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Debug prints out logs on debug level
|
// Debug prints out logs on debug level
|
||||||
@ -158,7 +158,19 @@ func Debug(args ...interface{}) {
|
|||||||
level: LDebug,
|
level: LDebug,
|
||||||
}
|
}
|
||||||
createLog(e)
|
createLog(e)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Formatted print for Debug
|
||||||
|
func Debugf(format string, args ...interface{}) {
|
||||||
|
output := fmt.Sprintf(format, args...)
|
||||||
|
e := Entry{
|
||||||
|
Timestamp: time.Now(),
|
||||||
|
Output: output,
|
||||||
|
File: fileInfo(2),
|
||||||
|
Level: "DEBUG",
|
||||||
|
level: LDebug,
|
||||||
|
}
|
||||||
|
createLog(e)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Info prints out logs on info level
|
// Info prints out logs on info level
|
||||||
@ -174,6 +186,19 @@ func Info(args ...interface{}) {
|
|||||||
createLog(e)
|
createLog(e)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Formatted print for Info
|
||||||
|
func Infof(format string, args ...interface{}) {
|
||||||
|
output := fmt.Sprintf(format, args...)
|
||||||
|
e := Entry{
|
||||||
|
Timestamp: time.Now(),
|
||||||
|
Output: output,
|
||||||
|
File: fileInfo(2),
|
||||||
|
Level: "INFO",
|
||||||
|
level: LInfo,
|
||||||
|
}
|
||||||
|
createLog(e)
|
||||||
|
}
|
||||||
|
|
||||||
// Warn prints out logs on warn level
|
// Warn prints out logs on warn level
|
||||||
func Warn(args ...interface{}) {
|
func Warn(args ...interface{}) {
|
||||||
output := fmt.Sprint(args...)
|
output := fmt.Sprint(args...)
|
||||||
@ -187,6 +212,19 @@ func Warn(args ...interface{}) {
|
|||||||
createLog(e)
|
createLog(e)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Formatted print for Warn
|
||||||
|
func Warnf(format string, args ...interface{}) {
|
||||||
|
output := fmt.Sprintf(format, args...)
|
||||||
|
e := Entry{
|
||||||
|
Timestamp: time.Now(),
|
||||||
|
Output: output,
|
||||||
|
File: fileInfo(2),
|
||||||
|
Level: "WARN",
|
||||||
|
level: LWarn,
|
||||||
|
}
|
||||||
|
createLog(e)
|
||||||
|
}
|
||||||
|
|
||||||
// Error prints out logs on error level
|
// Error prints out logs on error level
|
||||||
func Error(args ...interface{}) {
|
func Error(args ...interface{}) {
|
||||||
output := fmt.Sprint(args...)
|
output := fmt.Sprint(args...)
|
||||||
@ -200,6 +238,19 @@ func Error(args ...interface{}) {
|
|||||||
createLog(e)
|
createLog(e)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Formatted print for error
|
||||||
|
func Errorf(format string, args ...interface{}) {
|
||||||
|
output := fmt.Sprintf(format, args...)
|
||||||
|
e := Entry{
|
||||||
|
Timestamp: time.Now(),
|
||||||
|
Output: output,
|
||||||
|
File: fileInfo(2),
|
||||||
|
Level: "ERROR",
|
||||||
|
level: LError,
|
||||||
|
}
|
||||||
|
createLog(e)
|
||||||
|
}
|
||||||
|
|
||||||
// Panic prints out logs on panic level
|
// Panic prints out logs on panic level
|
||||||
func Panic(args ...interface{}) {
|
func Panic(args ...interface{}) {
|
||||||
output := fmt.Sprint(args...)
|
output := fmt.Sprint(args...)
|
||||||
@ -223,6 +274,29 @@ func Panic(args ...interface{}) {
|
|||||||
panic(errors.New(output))
|
panic(errors.New(output))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Formatted print for panic
|
||||||
|
func Panicf(format string, args ...interface{}) {
|
||||||
|
output := fmt.Sprintf(format, args...)
|
||||||
|
e := Entry{
|
||||||
|
Timestamp: time.Now(),
|
||||||
|
Output: output,
|
||||||
|
File: fileInfo(2),
|
||||||
|
Level: "PANIC",
|
||||||
|
level: LPanic,
|
||||||
|
}
|
||||||
|
createLog(e)
|
||||||
|
if len(args) >= 0 {
|
||||||
|
switch args[0].(type) {
|
||||||
|
case error:
|
||||||
|
panic(args[0])
|
||||||
|
default:
|
||||||
|
// falls through to default below
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Flush()
|
||||||
|
panic(errors.New(output))
|
||||||
|
}
|
||||||
|
|
||||||
// Fatal prints out logs on fatal level
|
// Fatal prints out logs on fatal level
|
||||||
func Fatal(args ...interface{}) {
|
func Fatal(args ...interface{}) {
|
||||||
output := fmt.Sprint(args...)
|
output := fmt.Sprint(args...)
|
||||||
@ -238,6 +312,21 @@ func Fatal(args ...interface{}) {
|
|||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Formatted print for fatal
|
||||||
|
func Fatalf(format string, args ...interface{}) {
|
||||||
|
output := fmt.Sprintf(format, args...)
|
||||||
|
e := Entry{
|
||||||
|
Timestamp: time.Now(),
|
||||||
|
Output: output,
|
||||||
|
File: fileInfo(2),
|
||||||
|
Level: "FATAL",
|
||||||
|
level: LFatal,
|
||||||
|
}
|
||||||
|
createLog(e)
|
||||||
|
Flush()
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
// fileInfo for getting which line in which file
|
// fileInfo for getting which line in which file
|
||||||
func fileInfo(skip int) string {
|
func fileInfo(skip int) string {
|
||||||
_, file, line, ok := runtime.Caller(skip)
|
_, file, line, ok := runtime.Caller(skip)
|
||||||
|
@ -5,6 +5,16 @@ import "time"
|
|||||||
type LogWriter chan Entry
|
type LogWriter chan Entry
|
||||||
type Level int
|
type Level int
|
||||||
|
|
||||||
|
const (
|
||||||
|
LTrace Level = iota
|
||||||
|
LDebug
|
||||||
|
LInfo
|
||||||
|
LWarn
|
||||||
|
LError
|
||||||
|
LPanic
|
||||||
|
LFatal
|
||||||
|
)
|
||||||
|
|
||||||
type Client struct {
|
type Client struct {
|
||||||
LogLevel Level `json:"level"`
|
LogLevel Level `json:"level"`
|
||||||
writer LogWriter
|
writer LogWriter
|
Loading…
x
Reference in New Issue
Block a user