mirror of
https://github.com/taigrr/log-socket
synced 2025-01-18 04:53:14 -08:00
Compare commits
18 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| a1e960366e | |||
|
|
430181e3a2 | ||
| 147a8cb30b | |||
|
|
8e044e3993 | ||
|
|
e97d37012e | ||
|
ebef59a9a8
|
|||
|
b3ceb12277
|
|||
|
45cad34fdc
|
|||
|
f0c16a0c56
|
|||
|
c18854598d
|
|||
|
63ed3a2ad7
|
|||
|
f51ec53a89
|
|||
|
d21c91379e
|
|||
|
af2116af48
|
|||
|
b65a10e7a8
|
|||
|
a07d3a11df
|
|||
|
5f18def7fa
|
|||
|
2c7cf0494e
|
12
.github/FUNDING.yml
vendored
Normal file
12
.github/FUNDING.yml
vendored
Normal file
@@ -0,0 +1,12 @@
|
||||
# These are supported funding model platforms
|
||||
|
||||
github: taigrr # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2]
|
||||
patreon: # Replace with a single Patreon username
|
||||
open_collective: # Replace with a single Open Collective username
|
||||
ko_fi: # Replace with a single Ko-fi username
|
||||
tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
|
||||
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
|
||||
liberapay: # Replace with a single Liberapay username
|
||||
issuehunt: # Replace with a single IssueHunt username
|
||||
otechie: # Replace with a single Otechie username
|
||||
custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2']
|
||||
12
LICENSE
Normal file
12
LICENSE
Normal file
@@ -0,0 +1,12 @@
|
||||
Copyright (C) 2019-2021 by Tai Groot <tai@taigrr.com>
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any
|
||||
purpose with or without fee is hereby granted.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
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.
|
||||
|
||||
@@ -3,10 +3,18 @@ package browser
|
||||
import (
|
||||
"html/template"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func LogSocketViewHandler(w http.ResponseWriter, r *http.Request) {
|
||||
homeTemplate.Execute(w, "ws://"+r.Host+r.URL.Path+"/ws")
|
||||
wsResource := r.Host + r.URL.Path
|
||||
if r.TLS != nil {
|
||||
wsResource = "wss://" + wsResource
|
||||
} else {
|
||||
wsResource = "ws://" + wsResource
|
||||
}
|
||||
wsResource = strings.TrimSuffix(wsResource, "/") + "/ws"
|
||||
homeTemplate.Execute(w, wsResource)
|
||||
}
|
||||
|
||||
var homeTemplate = template.Must(template.New("").Parse(`
|
||||
@@ -18,6 +26,7 @@ var homeTemplate = template.Must(template.New("").Parse(`
|
||||
<body>
|
||||
<center>
|
||||
<input type="text" id="search" onkeyup="filterTable()" placeholder="Filter...">
|
||||
<input type="checkbox" id="shouldScroll" checked>Enable Autoscroll<br>
|
||||
<table id="logHeaders" style="text-align:left; width:80%;" >
|
||||
<tbody>
|
||||
<tr class="header">
|
||||
@@ -183,6 +192,14 @@ function filterTable() {
|
||||
|
||||
}
|
||||
}
|
||||
function pageScroll() {
|
||||
if (document.getElementById('shouldScroll').checked) {
|
||||
document.getElementById('tableWrapper').scrollBy(0,10);
|
||||
|
||||
}
|
||||
setTimeout(pageScroll,10);
|
||||
}
|
||||
|
||||
document.getElementById("delete").addEventListener("click", function(){
|
||||
|
||||
clearTable()
|
||||
@@ -192,6 +209,7 @@ document.getElementById("download").addEventListener("click", function(){
|
||||
download(application+'.json',JSON.stringify(logs));
|
||||
}, false);
|
||||
openSocket();
|
||||
pageScroll();
|
||||
</script>
|
||||
|
||||
</footer>
|
||||
|
||||
2
go.mod
2
go.mod
@@ -1,5 +1,5 @@
|
||||
module github.com/taigrr/log-socket
|
||||
|
||||
go 1.16
|
||||
go 1.17
|
||||
|
||||
require github.com/gorilla/websocket v1.4.2
|
||||
|
||||
495
log/log.go
Normal file
495
log/log.go
Normal file
@@ -0,0 +1,495 @@
|
||||
package log
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"runtime"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
clients []*Client
|
||||
sliceTex sync.Mutex
|
||||
stderrClient *Client
|
||||
cleanup sync.Once
|
||||
stderrFinished chan bool
|
||||
)
|
||||
|
||||
func init() {
|
||||
stderrClient = CreateClient()
|
||||
stderrClient.SetLogLevel(LTrace)
|
||||
stderrFinished = make(chan bool, 1)
|
||||
go stderrClient.logStdErr()
|
||||
}
|
||||
|
||||
func (c *Client) logStdErr() {
|
||||
for {
|
||||
select {
|
||||
case e, more := <-c.writer:
|
||||
if e.level >= c.LogLevel {
|
||||
fmt.Fprintf(os.Stderr, "%s\t%s\t%s\t%s\n", e.Timestamp.String(), e.Level, e.Output, e.File)
|
||||
}
|
||||
if !more {
|
||||
stderrFinished <- true
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func CreateClient() *Client {
|
||||
var client Client
|
||||
client.initialized = true
|
||||
client.writer = make(LogWriter, 1000)
|
||||
sliceTex.Lock()
|
||||
clients = append(clients, &client)
|
||||
sliceTex.Unlock()
|
||||
return &client
|
||||
}
|
||||
|
||||
func Flush() {
|
||||
cleanup.Do(func() {
|
||||
close(stderrClient.writer)
|
||||
<-stderrFinished
|
||||
stderrClient.Destroy()
|
||||
})
|
||||
}
|
||||
|
||||
func (c *Client) Destroy() error {
|
||||
var otherClients []*Client
|
||||
if !c.initialized {
|
||||
panic(errors.New("Cannot delete uninitialized client, did you use CreateClient?"))
|
||||
}
|
||||
sliceTex.Lock()
|
||||
c.writer = nil
|
||||
c.initialized = false
|
||||
for _, x := range clients {
|
||||
if x.initialized {
|
||||
otherClients = append(otherClients, x)
|
||||
}
|
||||
}
|
||||
clients = otherClients
|
||||
sliceTex.Unlock()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Client) GetLogLevel() Level {
|
||||
if !c.initialized {
|
||||
panic(errors.New("Cannot get level for uninitialized client, use CreateClient instead"))
|
||||
}
|
||||
return c.LogLevel
|
||||
}
|
||||
|
||||
func createLog(e Entry) {
|
||||
sliceTex.Lock()
|
||||
for _, c := range clients {
|
||||
func(c *Client, e Entry) {
|
||||
select {
|
||||
case c.writer <- e:
|
||||
// try to clear out one of the older entries
|
||||
default:
|
||||
select {
|
||||
case <-c.writer:
|
||||
c.writer <- e
|
||||
default:
|
||||
}
|
||||
}
|
||||
}(c, e)
|
||||
}
|
||||
sliceTex.Unlock()
|
||||
}
|
||||
|
||||
func SetLogLevel(level Level) {
|
||||
stderrClient.LogLevel = level
|
||||
}
|
||||
|
||||
// SetLogLevel set log level of logger
|
||||
func (c *Client) SetLogLevel(level Level) {
|
||||
if !c.initialized {
|
||||
panic(errors.New("Cannot set level for uninitialized client, use CreateClient instead"))
|
||||
}
|
||||
c.LogLevel = level
|
||||
}
|
||||
|
||||
func (c *Client) Get() Entry {
|
||||
if !c.initialized {
|
||||
panic(errors.New("Cannot get logs for uninitialized client, did you use CreateClient?"))
|
||||
}
|
||||
return <-c.writer
|
||||
}
|
||||
|
||||
// Trace prints out logs on trace level
|
||||
func Trace(args ...interface{}) {
|
||||
output := fmt.Sprint(args...)
|
||||
e := Entry{
|
||||
Timestamp: time.Now(),
|
||||
Output: output,
|
||||
File: fileInfo(2),
|
||||
Level: "TRACE",
|
||||
level: LTrace,
|
||||
}
|
||||
createLog(e)
|
||||
}
|
||||
|
||||
// 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)
|
||||
}
|
||||
|
||||
// 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...)
|
||||
e := Entry{
|
||||
Timestamp: time.Now(),
|
||||
Output: output,
|
||||
File: fileInfo(2),
|
||||
Level: "DEBUG",
|
||||
level: LDebug,
|
||||
}
|
||||
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)
|
||||
}
|
||||
|
||||
// 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...)
|
||||
e := Entry{
|
||||
Timestamp: time.Now(),
|
||||
Output: output,
|
||||
File: fileInfo(2),
|
||||
Level: "INFO",
|
||||
level: LInfo,
|
||||
}
|
||||
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)
|
||||
}
|
||||
|
||||
// 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...)
|
||||
e := Entry{
|
||||
Timestamp: time.Now(),
|
||||
Output: output,
|
||||
File: fileInfo(2),
|
||||
Level: "NOTICE",
|
||||
level: LNotice,
|
||||
}
|
||||
createLog(e)
|
||||
}
|
||||
|
||||
// Formatted print for Info
|
||||
func Noticef(format string, args ...interface{}) {
|
||||
output := fmt.Sprintf(format, args...)
|
||||
e := Entry{
|
||||
Timestamp: time.Now(),
|
||||
Output: output,
|
||||
File: fileInfo(2),
|
||||
Level: "NOTICE",
|
||||
level: LNotice,
|
||||
}
|
||||
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...)
|
||||
e := Entry{
|
||||
Timestamp: time.Now(),
|
||||
Output: output,
|
||||
File: fileInfo(2),
|
||||
Level: "WARN",
|
||||
level: LWarn,
|
||||
}
|
||||
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)
|
||||
}
|
||||
|
||||
// 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...)
|
||||
e := Entry{
|
||||
Timestamp: time.Now(),
|
||||
Output: output,
|
||||
File: fileInfo(2),
|
||||
Level: "ERROR",
|
||||
level: LError,
|
||||
}
|
||||
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)
|
||||
}
|
||||
|
||||
// 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...)
|
||||
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))
|
||||
}
|
||||
|
||||
// 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))
|
||||
}
|
||||
|
||||
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...)
|
||||
e := Entry{
|
||||
Timestamp: time.Now(),
|
||||
Output: output,
|
||||
File: fileInfo(2),
|
||||
Level: "FATAL",
|
||||
level: LFatal,
|
||||
}
|
||||
createLog(e)
|
||||
Flush()
|
||||
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)
|
||||
}
|
||||
|
||||
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)
|
||||
if !ok {
|
||||
file = "<???>"
|
||||
line = 1
|
||||
} else {
|
||||
slash := strings.LastIndex(file, "/")
|
||||
if slash >= 0 {
|
||||
file = file[slash+1:]
|
||||
}
|
||||
}
|
||||
return fmt.Sprintf("%s:%d", file, line)
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package logger
|
||||
package log
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
@@ -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()
|
||||
}
|
||||
362
log/logger.go
Normal file
362
log/logger.go
Normal file
@@ -0,0 +1,362 @@
|
||||
package log
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
func (l *Logger) SetInfoDepth(depth int) {
|
||||
l.FileInfoDepth = depth
|
||||
}
|
||||
|
||||
// Trace prints out logs on trace level
|
||||
func (l Logger) Trace(args ...interface{}) {
|
||||
output := fmt.Sprint(args...)
|
||||
e := Entry{
|
||||
Timestamp: time.Now(),
|
||||
Output: output,
|
||||
File: fileInfo(l.FileInfoDepth),
|
||||
Level: "TRACE",
|
||||
level: LTrace,
|
||||
}
|
||||
createLog(e)
|
||||
}
|
||||
|
||||
// Formatted print for Trace
|
||||
func (l Logger) Tracef(format string, args ...interface{}) {
|
||||
output := fmt.Sprintf(format, args...)
|
||||
e := Entry{
|
||||
Timestamp: time.Now(),
|
||||
Output: output,
|
||||
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,
|
||||
}
|
||||
createLog(e)
|
||||
}
|
||||
|
||||
// Debug prints out logs on debug level
|
||||
func (l Logger) Debug(args ...interface{}) {
|
||||
output := fmt.Sprint(args...)
|
||||
e := Entry{
|
||||
Timestamp: time.Now(),
|
||||
Output: output,
|
||||
File: fileInfo(l.FileInfoDepth),
|
||||
Level: "DEBUG",
|
||||
level: LDebug,
|
||||
}
|
||||
createLog(e)
|
||||
}
|
||||
|
||||
// Formatted print for Debug
|
||||
func (l Logger) Debugf(format string, args ...interface{}) {
|
||||
output := fmt.Sprintf(format, args...)
|
||||
e := Entry{
|
||||
Timestamp: time.Now(),
|
||||
Output: output,
|
||||
File: fileInfo(l.FileInfoDepth),
|
||||
Level: "DEBUG",
|
||||
level: LDebug,
|
||||
}
|
||||
createLog(e)
|
||||
}
|
||||
|
||||
// Info prints out logs on info level
|
||||
func (l Logger) Info(args ...interface{}) {
|
||||
output := fmt.Sprint(args...)
|
||||
e := Entry{
|
||||
Timestamp: time.Now(),
|
||||
Output: output,
|
||||
File: fileInfo(l.FileInfoDepth),
|
||||
Level: "INFO",
|
||||
level: LInfo,
|
||||
}
|
||||
createLog(e)
|
||||
}
|
||||
|
||||
// Formatted print for Info
|
||||
func (l Logger) Infof(format string, args ...interface{}) {
|
||||
output := fmt.Sprintf(format, args...)
|
||||
e := Entry{
|
||||
Timestamp: time.Now(),
|
||||
Output: output,
|
||||
File: fileInfo(l.FileInfoDepth),
|
||||
Level: "INFO",
|
||||
level: LInfo,
|
||||
}
|
||||
createLog(e)
|
||||
}
|
||||
|
||||
// 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(l.FileInfoDepth),
|
||||
Level: "NOTICE",
|
||||
level: LNotice,
|
||||
}
|
||||
createLog(e)
|
||||
}
|
||||
|
||||
// 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(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,
|
||||
}
|
||||
createLog(e)
|
||||
}
|
||||
|
||||
// Warn prints out logs on warn level
|
||||
func (l Logger) Warn(args ...interface{}) {
|
||||
output := fmt.Sprint(args...)
|
||||
e := Entry{
|
||||
Timestamp: time.Now(),
|
||||
Output: output,
|
||||
File: fileInfo(l.FileInfoDepth),
|
||||
Level: "WARN",
|
||||
level: LWarn,
|
||||
}
|
||||
createLog(e)
|
||||
}
|
||||
|
||||
// Formatted print for Warn
|
||||
func (l Logger) Warnf(format string, args ...interface{}) {
|
||||
output := fmt.Sprintf(format, args...)
|
||||
e := Entry{
|
||||
Timestamp: time.Now(),
|
||||
Output: output,
|
||||
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,
|
||||
}
|
||||
createLog(e)
|
||||
}
|
||||
|
||||
// Error prints out logs on error level
|
||||
func (l Logger) Error(args ...interface{}) {
|
||||
output := fmt.Sprint(args...)
|
||||
e := Entry{
|
||||
Timestamp: time.Now(),
|
||||
Output: output,
|
||||
File: fileInfo(l.FileInfoDepth),
|
||||
Level: "ERROR",
|
||||
level: LError,
|
||||
}
|
||||
createLog(e)
|
||||
}
|
||||
|
||||
// Formatted print for error
|
||||
func (l Logger) Errorf(format string, args ...interface{}) {
|
||||
output := fmt.Sprintf(format, args...)
|
||||
e := Entry{
|
||||
Timestamp: time.Now(),
|
||||
Output: output,
|
||||
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,
|
||||
}
|
||||
createLog(e)
|
||||
}
|
||||
|
||||
// Panic prints out logs on panic level
|
||||
func (l Logger) Panic(args ...interface{}) {
|
||||
output := fmt.Sprint(args...)
|
||||
e := Entry{
|
||||
Timestamp: time.Now(),
|
||||
Output: output,
|
||||
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))
|
||||
}
|
||||
|
||||
// Formatted print for panic
|
||||
func (l Logger) Panicf(format string, args ...interface{}) {
|
||||
output := fmt.Sprintf(format, args...)
|
||||
e := Entry{
|
||||
Timestamp: time.Now(),
|
||||
Output: output,
|
||||
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,
|
||||
}
|
||||
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 (l Logger) Fatal(args ...interface{}) {
|
||||
output := fmt.Sprint(args...)
|
||||
e := Entry{
|
||||
Timestamp: time.Now(),
|
||||
Output: output,
|
||||
File: fileInfo(l.FileInfoDepth),
|
||||
Level: "FATAL",
|
||||
level: LFatal,
|
||||
}
|
||||
createLog(e)
|
||||
Flush()
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// Formatted print for fatal
|
||||
func (l Logger) Fatalf(format string, args ...interface{}) {
|
||||
output := fmt.Sprintf(format, args...)
|
||||
e := Entry{
|
||||
Timestamp: time.Now(),
|
||||
Output: output,
|
||||
File: fileInfo(l.FileInfoDepth),
|
||||
Level: "FATAL",
|
||||
level: LFatal,
|
||||
}
|
||||
createLog(e)
|
||||
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...)
|
||||
}
|
||||
@@ -1,10 +1,21 @@
|
||||
package logger
|
||||
package log
|
||||
|
||||
import "time"
|
||||
|
||||
type LogWriter chan Entry
|
||||
type Level int
|
||||
|
||||
const (
|
||||
LTrace Level = iota
|
||||
LDebug
|
||||
LInfo
|
||||
LNotice
|
||||
LWarn
|
||||
LError
|
||||
LPanic
|
||||
LFatal
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
LogLevel Level `json:"level"`
|
||||
writer LogWriter
|
||||
@@ -18,3 +29,7 @@ type Entry struct {
|
||||
Level string `json:"level"`
|
||||
level Level
|
||||
}
|
||||
|
||||
type Logger struct {
|
||||
FileInfoDepth int
|
||||
}
|
||||
254
logger/logger.go
254
logger/logger.go
@@ -1,254 +0,0 @@
|
||||
package logger
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"runtime"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
LTrace Level = iota
|
||||
LDebug
|
||||
LInfo
|
||||
LWarn
|
||||
LError
|
||||
LPanic
|
||||
LFatal
|
||||
)
|
||||
|
||||
var (
|
||||
clients []*Client
|
||||
sliceTex sync.Mutex
|
||||
stderrClient *Client
|
||||
cleanup sync.Once
|
||||
stderrFinished chan bool
|
||||
)
|
||||
|
||||
func init() {
|
||||
stderrClient = CreateClient()
|
||||
stderrClient.SetLogLevel(LTrace)
|
||||
stderrFinished = make(chan bool, 1)
|
||||
go stderrClient.logStdErr()
|
||||
}
|
||||
|
||||
func (c *Client) logStdErr() {
|
||||
for {
|
||||
select {
|
||||
case e, more := <-c.writer:
|
||||
if e.level >= c.LogLevel {
|
||||
fmt.Fprintf(os.Stderr, "%s\t%s\t%s\t%s\n", e.Timestamp.String(), e.Level, e.Output, e.File)
|
||||
}
|
||||
if !more {
|
||||
stderrFinished <- true
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func CreateClient() *Client {
|
||||
var client Client
|
||||
client.initialized = true
|
||||
client.writer = make(LogWriter, 1000)
|
||||
sliceTex.Lock()
|
||||
clients = append(clients, &client)
|
||||
sliceTex.Unlock()
|
||||
return &client
|
||||
}
|
||||
|
||||
func Flush() {
|
||||
cleanup.Do(func() {
|
||||
close(stderrClient.writer)
|
||||
<-stderrFinished
|
||||
stderrClient.Destroy()
|
||||
})
|
||||
}
|
||||
|
||||
func (c *Client) Destroy() error {
|
||||
var otherClients []*Client
|
||||
if !c.initialized {
|
||||
panic(errors.New("Cannot delete uninitialized client, did you use CreateClient?"))
|
||||
}
|
||||
sliceTex.Lock()
|
||||
c.writer = nil
|
||||
c.initialized = false
|
||||
for _, x := range clients {
|
||||
if x.initialized {
|
||||
otherClients = append(otherClients, x)
|
||||
}
|
||||
}
|
||||
clients = otherClients
|
||||
sliceTex.Unlock()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Client) GetLogLevel() Level {
|
||||
if !c.initialized {
|
||||
panic(errors.New("Cannot get level for uninitialized client, use CreateClient instead"))
|
||||
}
|
||||
return c.LogLevel
|
||||
}
|
||||
|
||||
func createLog(e Entry) {
|
||||
sliceTex.Lock()
|
||||
for _, c := range clients {
|
||||
func(c *Client, e Entry) {
|
||||
select {
|
||||
case c.writer <- e:
|
||||
// try to clear out one of the older entries
|
||||
default:
|
||||
select {
|
||||
case <-c.writer:
|
||||
c.writer <- e
|
||||
default:
|
||||
}
|
||||
}
|
||||
}(c, e)
|
||||
}
|
||||
sliceTex.Unlock()
|
||||
}
|
||||
|
||||
func SetLogLevel(level Level) {
|
||||
stderrClient.LogLevel = level
|
||||
}
|
||||
|
||||
// SetLogLevel set log level of logger
|
||||
func (c *Client) SetLogLevel(level Level) {
|
||||
if !c.initialized {
|
||||
panic(errors.New("Cannot set level for uninitialized client, use CreateClient instead"))
|
||||
}
|
||||
c.LogLevel = level
|
||||
}
|
||||
|
||||
func (c *Client) Get() Entry {
|
||||
if !c.initialized {
|
||||
panic(errors.New("Cannot get logs for uninitialized client, did you use CreateClient?"))
|
||||
}
|
||||
return <-c.writer
|
||||
}
|
||||
|
||||
// Trace prints out logs on trace level
|
||||
func Trace(args ...interface{}) {
|
||||
output := fmt.Sprint(args...)
|
||||
e := Entry{
|
||||
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
|
||||
func Debug(args ...interface{}) {
|
||||
output := fmt.Sprint(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...)
|
||||
e := Entry{
|
||||
Timestamp: time.Now(),
|
||||
Output: output,
|
||||
File: fileInfo(2),
|
||||
Level: "INFO",
|
||||
level: LInfo,
|
||||
}
|
||||
createLog(e)
|
||||
}
|
||||
|
||||
// Warn prints out logs on warn level
|
||||
func Warn(args ...interface{}) {
|
||||
output := fmt.Sprint(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...)
|
||||
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...)
|
||||
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...)
|
||||
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
|
||||
func fileInfo(skip int) string {
|
||||
_, file, line, ok := runtime.Caller(skip)
|
||||
if !ok {
|
||||
file = "<???>"
|
||||
line = 1
|
||||
} else {
|
||||
slash := strings.LastIndex(file, "/")
|
||||
if slash >= 0 {
|
||||
file = file[slash+1:]
|
||||
}
|
||||
}
|
||||
return fmt.Sprintf("%s:%d", file, line)
|
||||
}
|
||||
6
main.go
6
main.go
@@ -6,7 +6,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/taigrr/log-socket/browser"
|
||||
"github.com/taigrr/log-socket/logger"
|
||||
logger "github.com/taigrr/log-socket/log"
|
||||
"github.com/taigrr/log-socket/ws"
|
||||
)
|
||||
|
||||
@@ -19,14 +19,14 @@ func generateLogs() {
|
||||
logger.Debug("This is a debug log!")
|
||||
logger.Warn("This is a warn log!")
|
||||
logger.Error("This is an error log!")
|
||||
time.Sleep(10 * time.Second)
|
||||
time.Sleep(2 * time.Second)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
defer logger.Flush()
|
||||
flag.Parse()
|
||||
http.HandleFunc("/logs", ws.LogSocketHandler)
|
||||
http.HandleFunc("/ws", ws.LogSocketHandler)
|
||||
http.HandleFunc("/", browser.LogSocketViewHandler)
|
||||
go generateLogs()
|
||||
logger.Fatal(http.ListenAndServe(*addr, nil))
|
||||
|
||||
@@ -5,7 +5,7 @@ import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
"github.com/taigrr/log-socket/logger"
|
||||
logger "github.com/taigrr/log-socket/log"
|
||||
)
|
||||
|
||||
// var addr = flag.String("addr", "localhost:8080", "http service address")
|
||||
|
||||
Reference in New Issue
Block a user