mirror of
https://github.com/taigrr/log-socket
synced 2025-01-18 04:53:14 -08:00
Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| a1e960366e | |||
|
|
430181e3a2 | ||
| 147a8cb30b | |||
|
|
8e044e3993 | ||
|
|
e97d37012e | ||
|
ebef59a9a8
|
|||
|
b3ceb12277
|
|||
|
45cad34fdc
|
15
README.md
Normal file
15
README.md
Normal file
@@ -0,0 +1,15 @@
|
||||
log-socket
|
||||
==========
|
||||
`log-socket` is a drop-in replacement for Go's `log` package that allows for streaming of logs via WebSockets.
|
||||
|
||||
## Installation
|
||||
To install the library:
|
||||
`go get github.com/taigrr/log-socket`
|
||||
|
||||
## Running
|
||||
To run a demo of this library:
|
||||
`go run main.go`
|
||||
|
||||
This demo will do a sample of every log type and push results to `0.0.0.0:8080`. Once running, you can open a browser and navigate to
|
||||
`0.0.0.0:8080` to see an example implementation of how logs are streamed.
|
||||
|
||||
126
log/log.go
126
log/log.go
@@ -147,6 +147,19 @@ func Tracef(format string, args ...interface{}) {
|
||||
createLog(e)
|
||||
}
|
||||
|
||||
// Trace prints out logs on trace level with newline
|
||||
func Traceln(args ...interface{}) {
|
||||
output := fmt.Sprintln(args...)
|
||||
e := Entry{
|
||||
Timestamp: time.Now(),
|
||||
Output: output,
|
||||
File: fileInfo(2),
|
||||
Level: "TRACE",
|
||||
level: LTrace,
|
||||
}
|
||||
createLog(e)
|
||||
}
|
||||
|
||||
// Debug prints out logs on debug level
|
||||
func Debug(args ...interface{}) {
|
||||
output := fmt.Sprint(args...)
|
||||
@@ -173,6 +186,19 @@ func Debugf(format string, args ...interface{}) {
|
||||
createLog(e)
|
||||
}
|
||||
|
||||
// Debug prints out logs on debug level with a newline
|
||||
func Debugln(args ...interface{}) {
|
||||
output := fmt.Sprintln(args...)
|
||||
e := Entry{
|
||||
Timestamp: time.Now(),
|
||||
Output: output,
|
||||
File: fileInfo(2),
|
||||
Level: "DEBUG",
|
||||
level: LDebug,
|
||||
}
|
||||
createLog(e)
|
||||
}
|
||||
|
||||
// Info prints out logs on info level
|
||||
func Info(args ...interface{}) {
|
||||
output := fmt.Sprint(args...)
|
||||
@@ -199,6 +225,19 @@ func Infof(format string, args ...interface{}) {
|
||||
createLog(e)
|
||||
}
|
||||
|
||||
// Info prints out logs on info level with a newline
|
||||
func Infoln(args ...interface{}) {
|
||||
output := fmt.Sprintln(args...)
|
||||
e := Entry{
|
||||
Timestamp: time.Now(),
|
||||
Output: output,
|
||||
File: fileInfo(2),
|
||||
Level: "INFO",
|
||||
level: LInfo,
|
||||
}
|
||||
createLog(e)
|
||||
}
|
||||
|
||||
// Info prints out logs on info level
|
||||
func Notice(args ...interface{}) {
|
||||
output := fmt.Sprint(args...)
|
||||
@@ -225,6 +264,19 @@ func Noticef(format string, args ...interface{}) {
|
||||
createLog(e)
|
||||
}
|
||||
|
||||
// Info prints out logs on info level with a newline
|
||||
func Noticeln(args ...interface{}) {
|
||||
output := fmt.Sprintln(args...)
|
||||
e := Entry{
|
||||
Timestamp: time.Now(),
|
||||
Output: output,
|
||||
File: fileInfo(2),
|
||||
Level: "NOTICE",
|
||||
level: LNotice,
|
||||
}
|
||||
createLog(e)
|
||||
}
|
||||
|
||||
// Warn prints out logs on warn level
|
||||
func Warn(args ...interface{}) {
|
||||
output := fmt.Sprint(args...)
|
||||
@@ -251,6 +303,19 @@ func Warnf(format string, args ...interface{}) {
|
||||
createLog(e)
|
||||
}
|
||||
|
||||
// Newline print for Warn
|
||||
func Warnln(args ...interface{}) {
|
||||
output := fmt.Sprintln(args...)
|
||||
e := Entry{
|
||||
Timestamp: time.Now(),
|
||||
Output: output,
|
||||
File: fileInfo(2),
|
||||
Level: "WARN",
|
||||
level: LWarn,
|
||||
}
|
||||
createLog(e)
|
||||
}
|
||||
|
||||
// Error prints out logs on error level
|
||||
func Error(args ...interface{}) {
|
||||
output := fmt.Sprint(args...)
|
||||
@@ -277,6 +342,19 @@ func Errorf(format string, args ...interface{}) {
|
||||
createLog(e)
|
||||
}
|
||||
|
||||
// Error prints out logs on error level with a newline
|
||||
func Errorln(args ...interface{}) {
|
||||
output := fmt.Sprintln(args...)
|
||||
e := Entry{
|
||||
Timestamp: time.Now(),
|
||||
Output: output,
|
||||
File: fileInfo(2),
|
||||
Level: "ERROR",
|
||||
level: LError,
|
||||
}
|
||||
createLog(e)
|
||||
}
|
||||
|
||||
// Panic prints out logs on panic level
|
||||
func Panic(args ...interface{}) {
|
||||
output := fmt.Sprint(args...)
|
||||
@@ -323,6 +401,28 @@ func Panicf(format string, args ...interface{}) {
|
||||
panic(errors.New(output))
|
||||
}
|
||||
|
||||
func Panicln(args ...interface{}) {
|
||||
output := fmt.Sprintln(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
|
||||
func Fatal(args ...interface{}) {
|
||||
output := fmt.Sprint(args...)
|
||||
@@ -353,6 +453,32 @@ func Fatalf(format string, args ...interface{}) {
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
func Fatalln(args ...interface{}) {
|
||||
output := fmt.Sprintln(args...)
|
||||
e := Entry{
|
||||
Timestamp: time.Now(),
|
||||
Output: output,
|
||||
File: fileInfo(2),
|
||||
Level: "FATAL",
|
||||
level: LFatal,
|
||||
}
|
||||
createLog(e)
|
||||
Flush()
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
func Print(args ...interface{}) {
|
||||
Info(args...)
|
||||
}
|
||||
|
||||
func Printf(format string, args ...interface{}) {
|
||||
Infof(format, args...)
|
||||
}
|
||||
|
||||
func Println(args ...interface{}) {
|
||||
Infoln(args...)
|
||||
}
|
||||
|
||||
// fileInfo for getting which line in which file
|
||||
func fileInfo(skip int) string {
|
||||
_, file, line, ok := runtime.Caller(skip)
|
||||
|
||||
@@ -86,6 +86,15 @@ func TestInfo(t *testing.T) {
|
||||
// }
|
||||
}
|
||||
|
||||
// Print prints out logs on info level
|
||||
func TestPrint(t *testing.T) {
|
||||
// if logLevel >= LInfo {
|
||||
// entry := logger.WithFields(logrus.Fields{})
|
||||
// entry.Data["file"] = fileInfo(2)
|
||||
// entry.Info(args...)
|
||||
// }
|
||||
}
|
||||
|
||||
// Warn prints out logs on warn level
|
||||
func TestWarn(t *testing.T) {
|
||||
// if logLevel >= LWarn {
|
||||
@@ -121,6 +130,7 @@ func TestPanic(t *testing.T) {
|
||||
// entry.Panic(args...)
|
||||
// }
|
||||
}
|
||||
|
||||
func TestFlush(t *testing.T) {
|
||||
defer Flush()
|
||||
}
|
||||
|
||||
190
log/logger.go
190
log/logger.go
@@ -7,13 +7,17 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
func (l *Logger) SetInfoDepth(depth int) {
|
||||
l.FileInfoDepth = depth
|
||||
}
|
||||
|
||||
// Trace prints out logs on trace level
|
||||
func (_ Logger) Trace(args ...interface{}) {
|
||||
func (l Logger) Trace(args ...interface{}) {
|
||||
output := fmt.Sprint(args...)
|
||||
e := Entry{
|
||||
Timestamp: time.Now(),
|
||||
Output: output,
|
||||
File: fileInfo(2),
|
||||
File: fileInfo(l.FileInfoDepth),
|
||||
Level: "TRACE",
|
||||
level: LTrace,
|
||||
}
|
||||
@@ -21,12 +25,25 @@ func (_ Logger) Trace(args ...interface{}) {
|
||||
}
|
||||
|
||||
// Formatted print for Trace
|
||||
func (_ Logger) Tracef(format string, args ...interface{}) {
|
||||
func (l Logger) Tracef(format string, args ...interface{}) {
|
||||
output := fmt.Sprintf(format, args...)
|
||||
e := Entry{
|
||||
Timestamp: time.Now(),
|
||||
Output: output,
|
||||
File: fileInfo(2),
|
||||
File: fileInfo(l.FileInfoDepth),
|
||||
Level: "TRACE",
|
||||
level: LTrace,
|
||||
}
|
||||
createLog(e)
|
||||
}
|
||||
|
||||
// Trace prints out logs on trace level with newline
|
||||
func (l Logger) Traceln(args ...interface{}) {
|
||||
output := fmt.Sprintln(args...)
|
||||
e := Entry{
|
||||
Timestamp: time.Now(),
|
||||
Output: output,
|
||||
File: fileInfo(l.FileInfoDepth),
|
||||
Level: "TRACE",
|
||||
level: LTrace,
|
||||
}
|
||||
@@ -34,12 +51,12 @@ func (_ Logger) Tracef(format string, args ...interface{}) {
|
||||
}
|
||||
|
||||
// Debug prints out logs on debug level
|
||||
func (_ Logger) Debug(args ...interface{}) {
|
||||
func (l Logger) Debug(args ...interface{}) {
|
||||
output := fmt.Sprint(args...)
|
||||
e := Entry{
|
||||
Timestamp: time.Now(),
|
||||
Output: output,
|
||||
File: fileInfo(2),
|
||||
File: fileInfo(l.FileInfoDepth),
|
||||
Level: "DEBUG",
|
||||
level: LDebug,
|
||||
}
|
||||
@@ -47,12 +64,12 @@ func (_ Logger) Debug(args ...interface{}) {
|
||||
}
|
||||
|
||||
// Formatted print for Debug
|
||||
func (_ Logger) Debugf(format string, args ...interface{}) {
|
||||
func (l Logger) Debugf(format string, args ...interface{}) {
|
||||
output := fmt.Sprintf(format, args...)
|
||||
e := Entry{
|
||||
Timestamp: time.Now(),
|
||||
Output: output,
|
||||
File: fileInfo(2),
|
||||
File: fileInfo(l.FileInfoDepth),
|
||||
Level: "DEBUG",
|
||||
level: LDebug,
|
||||
}
|
||||
@@ -60,12 +77,12 @@ func (_ Logger) Debugf(format string, args ...interface{}) {
|
||||
}
|
||||
|
||||
// Info prints out logs on info level
|
||||
func (_ Logger) Info(args ...interface{}) {
|
||||
func (l Logger) Info(args ...interface{}) {
|
||||
output := fmt.Sprint(args...)
|
||||
e := Entry{
|
||||
Timestamp: time.Now(),
|
||||
Output: output,
|
||||
File: fileInfo(2),
|
||||
File: fileInfo(l.FileInfoDepth),
|
||||
Level: "INFO",
|
||||
level: LInfo,
|
||||
}
|
||||
@@ -73,38 +90,64 @@ func (_ Logger) Info(args ...interface{}) {
|
||||
}
|
||||
|
||||
// Formatted print for Info
|
||||
func (_ Logger) Infof(format string, args ...interface{}) {
|
||||
func (l Logger) Infof(format string, args ...interface{}) {
|
||||
output := fmt.Sprintf(format, args...)
|
||||
e := Entry{
|
||||
Timestamp: time.Now(),
|
||||
Output: output,
|
||||
File: fileInfo(2),
|
||||
File: fileInfo(l.FileInfoDepth),
|
||||
Level: "INFO",
|
||||
level: LInfo,
|
||||
}
|
||||
createLog(e)
|
||||
}
|
||||
|
||||
// Info prints out logs on info level
|
||||
func (_ Logger) Notice(args ...interface{}) {
|
||||
// Info prints out logs on info level with newline
|
||||
func (l Logger) Infoln(args ...interface{}) {
|
||||
output := fmt.Sprintln(args...)
|
||||
e := Entry{
|
||||
Timestamp: time.Now(),
|
||||
Output: output,
|
||||
File: fileInfo(l.FileInfoDepth),
|
||||
Level: "INFO",
|
||||
level: LInfo,
|
||||
}
|
||||
createLog(e)
|
||||
}
|
||||
|
||||
// Notice prints out logs on notice level
|
||||
func (l Logger) Notice(args ...interface{}) {
|
||||
output := fmt.Sprint(args...)
|
||||
e := Entry{
|
||||
Timestamp: time.Now(),
|
||||
Output: output,
|
||||
File: fileInfo(2),
|
||||
File: fileInfo(l.FileInfoDepth),
|
||||
Level: "NOTICE",
|
||||
level: LNotice,
|
||||
}
|
||||
createLog(e)
|
||||
}
|
||||
|
||||
// Formatted print for Info
|
||||
func (_ Logger) Noticef(format string, args ...interface{}) {
|
||||
// Formatted print for Notice
|
||||
func (l Logger) Noticef(format string, args ...interface{}) {
|
||||
output := fmt.Sprintf(format, args...)
|
||||
e := Entry{
|
||||
Timestamp: time.Now(),
|
||||
Output: output,
|
||||
File: fileInfo(2),
|
||||
File: fileInfo(l.FileInfoDepth),
|
||||
Level: "NOTICE",
|
||||
level: LNotice,
|
||||
}
|
||||
createLog(e)
|
||||
}
|
||||
|
||||
// Notice prints out logs on notice level with newline
|
||||
func (l Logger) Noticeln(args ...interface{}) {
|
||||
output := fmt.Sprintln(args...)
|
||||
e := Entry{
|
||||
Timestamp: time.Now(),
|
||||
Output: output,
|
||||
File: fileInfo(l.FileInfoDepth),
|
||||
Level: "NOTICE",
|
||||
level: LNotice,
|
||||
}
|
||||
@@ -112,12 +155,12 @@ func (_ Logger) Noticef(format string, args ...interface{}) {
|
||||
}
|
||||
|
||||
// Warn prints out logs on warn level
|
||||
func (_ Logger) Warn(args ...interface{}) {
|
||||
func (l Logger) Warn(args ...interface{}) {
|
||||
output := fmt.Sprint(args...)
|
||||
e := Entry{
|
||||
Timestamp: time.Now(),
|
||||
Output: output,
|
||||
File: fileInfo(2),
|
||||
File: fileInfo(l.FileInfoDepth),
|
||||
Level: "WARN",
|
||||
level: LWarn,
|
||||
}
|
||||
@@ -125,12 +168,25 @@ func (_ Logger) Warn(args ...interface{}) {
|
||||
}
|
||||
|
||||
// Formatted print for Warn
|
||||
func (_ Logger) Warnf(format string, args ...interface{}) {
|
||||
func (l Logger) Warnf(format string, args ...interface{}) {
|
||||
output := fmt.Sprintf(format, args...)
|
||||
e := Entry{
|
||||
Timestamp: time.Now(),
|
||||
Output: output,
|
||||
File: fileInfo(2),
|
||||
File: fileInfo(l.FileInfoDepth),
|
||||
Level: "WARN",
|
||||
level: LWarn,
|
||||
}
|
||||
createLog(e)
|
||||
}
|
||||
|
||||
// Warn prints out logs on warn level with a newline
|
||||
func (l Logger) Warnln(args ...interface{}) {
|
||||
output := fmt.Sprintln(args...)
|
||||
e := Entry{
|
||||
Timestamp: time.Now(),
|
||||
Output: output,
|
||||
File: fileInfo(l.FileInfoDepth),
|
||||
Level: "WARN",
|
||||
level: LWarn,
|
||||
}
|
||||
@@ -138,12 +194,12 @@ func (_ Logger) Warnf(format string, args ...interface{}) {
|
||||
}
|
||||
|
||||
// Error prints out logs on error level
|
||||
func (_ Logger) Error(args ...interface{}) {
|
||||
func (l Logger) Error(args ...interface{}) {
|
||||
output := fmt.Sprint(args...)
|
||||
e := Entry{
|
||||
Timestamp: time.Now(),
|
||||
Output: output,
|
||||
File: fileInfo(2),
|
||||
File: fileInfo(l.FileInfoDepth),
|
||||
Level: "ERROR",
|
||||
level: LError,
|
||||
}
|
||||
@@ -151,12 +207,25 @@ func (_ Logger) Error(args ...interface{}) {
|
||||
}
|
||||
|
||||
// Formatted print for error
|
||||
func (_ Logger) Errorf(format string, args ...interface{}) {
|
||||
func (l Logger) Errorf(format string, args ...interface{}) {
|
||||
output := fmt.Sprintf(format, args...)
|
||||
e := Entry{
|
||||
Timestamp: time.Now(),
|
||||
Output: output,
|
||||
File: fileInfo(2),
|
||||
File: fileInfo(l.FileInfoDepth),
|
||||
Level: "ERROR",
|
||||
level: LError,
|
||||
}
|
||||
createLog(e)
|
||||
}
|
||||
|
||||
// Error prints out logs on error level with a new line
|
||||
func (l Logger) Errorln(args ...interface{}) {
|
||||
output := fmt.Sprintln(args...)
|
||||
e := Entry{
|
||||
Timestamp: time.Now(),
|
||||
Output: output,
|
||||
File: fileInfo(l.FileInfoDepth),
|
||||
Level: "ERROR",
|
||||
level: LError,
|
||||
}
|
||||
@@ -164,12 +233,12 @@ func (_ Logger) Errorf(format string, args ...interface{}) {
|
||||
}
|
||||
|
||||
// Panic prints out logs on panic level
|
||||
func (_ Logger) Panic(args ...interface{}) {
|
||||
func (l Logger) Panic(args ...interface{}) {
|
||||
output := fmt.Sprint(args...)
|
||||
e := Entry{
|
||||
Timestamp: time.Now(),
|
||||
Output: output,
|
||||
File: fileInfo(2),
|
||||
File: fileInfo(l.FileInfoDepth),
|
||||
Level: "PANIC",
|
||||
level: LPanic,
|
||||
}
|
||||
@@ -187,12 +256,35 @@ func (_ Logger) Panic(args ...interface{}) {
|
||||
}
|
||||
|
||||
// Formatted print for panic
|
||||
func (_ Logger) Panicf(format string, args ...interface{}) {
|
||||
func (l Logger) Panicf(format string, args ...interface{}) {
|
||||
output := fmt.Sprintf(format, args...)
|
||||
e := Entry{
|
||||
Timestamp: time.Now(),
|
||||
Output: output,
|
||||
File: fileInfo(2),
|
||||
File: fileInfo(l.FileInfoDepth),
|
||||
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))
|
||||
}
|
||||
|
||||
// Panic prints out logs on panic level with a newline
|
||||
func (l Logger) Panicln(args ...interface{}) {
|
||||
output := fmt.Sprintln(args...)
|
||||
e := Entry{
|
||||
Timestamp: time.Now(),
|
||||
Output: output,
|
||||
File: fileInfo(l.FileInfoDepth),
|
||||
Level: "PANIC",
|
||||
level: LPanic,
|
||||
}
|
||||
@@ -210,12 +302,12 @@ func (_ Logger) Panicf(format string, args ...interface{}) {
|
||||
}
|
||||
|
||||
// Fatal prints out logs on fatal level
|
||||
func (_ Logger) Fatal(args ...interface{}) {
|
||||
func (l Logger) Fatal(args ...interface{}) {
|
||||
output := fmt.Sprint(args...)
|
||||
e := Entry{
|
||||
Timestamp: time.Now(),
|
||||
Output: output,
|
||||
File: fileInfo(2),
|
||||
File: fileInfo(l.FileInfoDepth),
|
||||
Level: "FATAL",
|
||||
level: LFatal,
|
||||
}
|
||||
@@ -225,12 +317,12 @@ func (_ Logger) Fatal(args ...interface{}) {
|
||||
}
|
||||
|
||||
// Formatted print for fatal
|
||||
func (_ Logger) Fatalf(format string, args ...interface{}) {
|
||||
func (l Logger) Fatalf(format string, args ...interface{}) {
|
||||
output := fmt.Sprintf(format, args...)
|
||||
e := Entry{
|
||||
Timestamp: time.Now(),
|
||||
Output: output,
|
||||
File: fileInfo(2),
|
||||
File: fileInfo(l.FileInfoDepth),
|
||||
Level: "FATAL",
|
||||
level: LFatal,
|
||||
}
|
||||
@@ -238,3 +330,33 @@ func (_ Logger) Fatalf(format string, args ...interface{}) {
|
||||
Flush()
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// Fatal prints fatal level with a new line
|
||||
func (l Logger) Fatalln(args ...interface{}) {
|
||||
output := fmt.Sprintln(args...)
|
||||
e := Entry{
|
||||
Timestamp: time.Now(),
|
||||
Output: output,
|
||||
File: fileInfo(l.FileInfoDepth),
|
||||
Level: "FATAL",
|
||||
level: LFatal,
|
||||
}
|
||||
createLog(e)
|
||||
Flush()
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// Handles print to info
|
||||
func (l Logger) Print(args ...interface{}) {
|
||||
l.Info(args...)
|
||||
}
|
||||
|
||||
// Handles formatted print to info
|
||||
func (l Logger) Printf(format string, args ...interface{}) {
|
||||
l.Infof(format, args...)
|
||||
}
|
||||
|
||||
// Handles print to info with new line
|
||||
func (l Logger) Println(args ...interface{}) {
|
||||
l.Infoln(args...)
|
||||
}
|
||||
|
||||
@@ -31,4 +31,5 @@ type Entry struct {
|
||||
}
|
||||
|
||||
type Logger struct {
|
||||
FileInfoDepth int
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user