1
0
mirror of https://github.com/taigrr/log-socket synced 2025-01-18 04:53:14 -08:00

Adds Flush method

This commit is contained in:
Tai Groot 2021-04-08 19:19:24 -07:00
parent 9ff091038a
commit 65ff9008da
Signed by: taigrr
GPG Key ID: D00C269A87614812
3 changed files with 122 additions and 44 deletions

142
logger.go
View File

@ -1,12 +1,13 @@
package lambo_log_socket package lambo_log_socket
import ( import (
"errors"
"fmt" "fmt"
"os"
"runtime" "runtime"
"strings" "strings"
"sync" "sync"
"time"
"github.com/sirupsen/logrus"
) )
type Level int type Level int
@ -21,24 +22,30 @@ const (
LFatal LFatal
) )
var logger = logrus.New() var (
var clients []*Client clients []*Client
var sliceTex sync.Mutex sliceTex sync.Mutex
stderrClient *Client
cleanup sync.Once
stderrFinished chan bool
)
func init() { func init() {
stderrClient := CreateClient() stderrClient = CreateClient()
stderrClient.SetLogLevel(LTrace)
stderrFinished = make(chan bool, 1)
go stderrClient.logStdErr() go stderrClient.logStdErr()
logger.SetLevel(logrus.DebugLevel)
} }
func (c *Client) logStdErr() { func (c *Client) logStdErr() {
for { for {
select { select {
case entry, more := <-c.writer: case e, more := <-c.writer:
if c.LogLevel >= entry.level { if e.level >= c.LogLevel {
fmt.Println(entry.Output) fmt.Fprintf(os.Stderr, "%s\t%s\t%s\t%s\n", e.Timestamp.String(), e.Level, e.Output, e.File)
} }
if !more { if !more {
stderrFinished <- true
return return
} }
} }
@ -54,6 +61,13 @@ func CreateClient() *Client {
return &client return &client
} }
func Flush() {
cleanup.Do(func() {
close(stderrClient.writer)
<-stderrFinished
})
}
func (c *Client) Destroy() error { func (c *Client) Destroy() error {
var otherClients []*Client var otherClients []*Client
sliceTex.Lock() sliceTex.Lock()
@ -76,7 +90,7 @@ func (c *Client) GetLogLevel() Level {
func createLog(e Entry) { func createLog(e Entry) {
sliceTex.Lock() sliceTex.Lock()
for _, c := range clients { for _, c := range clients {
go func(c *Client, e Entry) { func(c *Client, e Entry) {
select { select {
case c.writer <- e: case c.writer <- e:
default: default:
@ -93,51 +107,107 @@ func (c *Client) SetLogLevel(level Level) {
// Trace prints out logs on trace level // Trace prints out logs on trace level
func Trace(args ...interface{}) { func Trace(args ...interface{}) {
entry := logger.WithFields(logrus.Fields{}) output := fmt.Sprint(args...)
entry.Data["file"] = fileInfo(2) e := Entry{
entry.Debug(args...) Timestamp: time.Now(),
Output: output,
File: fileInfo(2),
Level: "TRACE",
level: LTrace,
}
createLog(e)
// entry := logger.WithFields(logrus.Fields{})
// entry.Data["file"] = fileInfo(2)
// entry.Debug(args...)
} }
// Debug prints out logs on debug level // Debug prints out logs on debug level
func Debug(args ...interface{}) { func Debug(args ...interface{}) {
entry := logger.WithFields(logrus.Fields{}) output := fmt.Sprint(args...)
entry.Data["file"] = fileInfo(2) e := Entry{
entry.Debug(args...) 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
func Info(args ...interface{}) { func Info(args ...interface{}) {
entry := logger.WithFields(logrus.Fields{}) output := fmt.Sprint(args...)
entry.Data["file"] = fileInfo(2) e := Entry{
entry.Info(args...) 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{}) {
entry := logger.WithFields(logrus.Fields{}) output := fmt.Sprint(args...)
entry.Data["file"] = fileInfo(2) e := Entry{
entry.Warn(args...) 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{}) {
entry := logger.WithFields(logrus.Fields{}) output := fmt.Sprint(args...)
entry.Data["file"] = fileInfo(2) e := Entry{
entry.Error(args...) Timestamp: time.Now(),
} Output: output,
File: fileInfo(2),
// Fatal prints out logs on fatal level Level: "ERROR",
func Fatal(args ...interface{}) { level: LError,
entry := logger.WithFields(logrus.Fields{}) }
entry.Data["file"] = fileInfo(2) createLog(e)
entry.Fatal(args...)
} }
// Panic prints out logs on panic level // Panic prints out logs on panic level
func Panic(args ...interface{}) { func Panic(args ...interface{}) {
entry := logger.WithFields(logrus.Fields{}) output := fmt.Sprint(args...)
entry.Data["file"] = fileInfo(2) e := Entry{
entry.Panic(args...) 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
}
}
panic(errors.New(output))
}
// Fatal prints out logs on fatal level
func Fatal(args ...interface{}) {
output := fmt.Sprint(args...)
e := Entry{
Timestamp: time.Now(),
Output: output,
File: fileInfo(2),
Level: "FATAL",
level: LFatal,
}
createLog(e)
os.Exit(1)
} }
// fileInfo for getting which line in which file // fileInfo for getting which line in which file

View File

@ -25,15 +25,17 @@ func TestSetLogLevel(t *testing.T) {
func TestTrace(t *testing.T) { func TestTrace(t *testing.T) {
var c Client var c Client
c.SetLogLevel(LTrace) c.SetLogLevel(LTrace)
// if logLevel >= LTrace { Trace("Testing trace!")
// entry := logger.WithFields(logrus.Fields{}) Trace("Testing trace!")
// entry.Data["file"] = fileInfo(2) Trace("Testing trace!")
// entry.Debug(args...) Trace("Testing trace!")
// } Trace("Testing trace!")
} }
// Debug prints out logs on debug level // Debug prints out logs on debug level
func TestDebug(t *testing.T) { func TestDebug(t *testing.T) {
Debug("Test of Debug")
// if logLevel >= LDebug { // if logLevel >= LDebug {
// entry := logger.WithFields(logrus.Fields{}) // entry := logger.WithFields(logrus.Fields{})
// entry.Data["file"] = fileInfo(2) // entry.Data["file"] = fileInfo(2)
@ -85,3 +87,6 @@ func TestPanic(t *testing.T) {
// entry.Panic(args...) // entry.Panic(args...)
// } // }
} }
func TestFlush(t *testing.T) {
defer Flush()
}

View File

@ -1,5 +1,7 @@
package lambo_log_socket package lambo_log_socket
import "time"
type LogWriter chan Entry type LogWriter chan Entry
type Client struct { type Client struct {
@ -8,8 +10,9 @@ type Client struct {
} }
type Entry struct { type Entry struct {
Timestamp string `json:"timestamp"` Timestamp time.Time `json:"timestamp"`
Output string `json:"output"` Output string `json:"output"`
File string `json:"file"`
Level string `json:"level"` Level string `json:"level"`
level Level level Level
} }