From 9c747daf9e9a144612f63fbafbaa343ead33a205 Mon Sep 17 00:00:00 2001 From: Noah Petherbridge Date: Sat, 4 May 2024 19:15:15 -0700 Subject: [PATCH] Added TeeFile helper to log to stdout and file --- README.md | 2 +- config.go | 6 +++--- go.mod | 13 +++++++++++++ go.sum | 8 ++++++++ levels.go | 3 +-- tee.go | 35 +++++++++++++++++++++++++++++++++++ terminal_js.go | 2 ++ 7 files changed, 63 insertions(+), 6 deletions(-) create mode 100644 go.mod create mode 100644 go.sum create mode 100644 tee.go diff --git a/README.md b/README.md index 6190cec..6772527 100644 --- a/README.md +++ b/README.md @@ -49,7 +49,7 @@ func main() { ``` The MIT License (MIT) -Copyright (c) 2017-18 Noah Petherbridge +Copyright (c) 2017-24 Noah Petherbridge Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/config.go b/config.go index 5887e5c..e197e01 100644 --- a/config.go +++ b/config.go @@ -18,7 +18,7 @@ type Config struct { // Where to write the log messages to? If not defined with a custom io.Writer, // the default goes to standard output for Debug and Info messages and // standard error for warnings, errors, and fatal messages. - Writer *io.Writer + Writer io.Writer // How do you want to format your log lines? This should be a Go text format // string, with the following variable placeholders: @@ -56,8 +56,8 @@ func DefaultConfig() *Config { // keys are not defined (or have zero-values), the default value for the key will // be used instead: // -// Format -// TimeFormat +// Format +// TimeFormat func (l *Logger) Configure(cfg *Config) { // Important keys and their defaults. if cfg.Format == "" { diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..c57a769 --- /dev/null +++ b/go.mod @@ -0,0 +1,13 @@ +module git.kirsle.net/go/log + +go 1.22.0 + +require ( + github.com/tomnomnom/xtermcolor v0.0.0-20160428124646-b78803f00a7e + golang.org/x/crypto v0.22.0 +) + +require ( + golang.org/x/sys v0.19.0 // indirect + golang.org/x/term v0.19.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..0a3f7d3 --- /dev/null +++ b/go.sum @@ -0,0 +1,8 @@ +github.com/tomnomnom/xtermcolor v0.0.0-20160428124646-b78803f00a7e h1:Ee+VZw13r9NTOMnwTPs6O5KZ0MJU54hsxu9FpZ4pQ10= +github.com/tomnomnom/xtermcolor v0.0.0-20160428124646-b78803f00a7e/go.mod h1:fSIW/szJHsRts/4U8wlMPhs+YqJC+7NYR+Qqb1uJVpA= +golang.org/x/crypto v0.22.0 h1:g1v0xeRhjcugydODzvb3mEM9SQ0HGp9s/nh3COQ/C30= +golang.org/x/crypto v0.22.0/go.mod h1:vr6Su+7cTlO45qkww3VDJlzDn0ctJvRgYbC2NvXHt+M= +golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o= +golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/term v0.19.0 h1:+ThwsDv+tYfnJFhF4L8jITxu1tdTWRTZpdsWgEgjL6Q= +golang.org/x/term v0.19.0/go.mod h1:2CuTdWZ7KHSQwUzKva0cbMg6q2DMI3Mmxp+gKJbskEk= diff --git a/levels.go b/levels.go index 0903822..7f69135 100644 --- a/levels.go +++ b/levels.go @@ -32,8 +32,7 @@ func (l *Logger) emit(level logLevel, tmpl string, args ...interface{}) { // If we have a log writer, send it there. if l.Config.Writer != nil { - // TODO - // l.Config.Writer.Write(message) + fmt.Fprintln(l.Config.Writer, message) } else { // No writer given so we default to standard out/error. if level <= InfoLevel { diff --git a/tee.go b/tee.go new file mode 100644 index 0000000..3ab5f8b --- /dev/null +++ b/tee.go @@ -0,0 +1,35 @@ +package log + +import ( + "fmt" + "io" + "os" + "regexp" +) + +// FileTee can write logs to a file on disk as well as standard output. +type FileTee struct { + fh io.Writer +} + +var RegexpANSIColorCode = regexp.MustCompile(`\x1B\[[0-9;]+m`) + +func NewFileTee(filename string) (*FileTee, error) { + fh, err := os.Create(filename) + if err != nil { + return nil, err + } + + return &FileTee{ + fh: fh, + }, nil +} + +func (ft *FileTee) Write(p []byte) (n int, err error) { + var ( + text = string(p) + noColor = RegexpANSIColorCode.ReplaceAllString(text, "") + ) + fmt.Fprintf(os.Stdout, string(p)) + return fmt.Fprintf(ft.fh, noColor) +} diff --git a/terminal_js.go b/terminal_js.go index 0d1092c..522f1a6 100644 --- a/terminal_js.go +++ b/terminal_js.go @@ -1,4 +1,6 @@ +//go:build js && wasm // +build js,wasm + package log // WASM builds don't have a terminal to query so interactive=false for plain