Added TeeFile helper to log to stdout and file

This commit is contained in:
Noah 2024-05-04 19:15:15 -07:00
parent 70ac284894
commit 9c747daf9e
7 changed files with 63 additions and 6 deletions

View File

@ -49,7 +49,7 @@ func main() {
``` ```
The MIT License (MIT) 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 Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal

View File

@ -18,7 +18,7 @@ type Config struct {
// Where to write the log messages to? If not defined with a custom io.Writer, // 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 // the default goes to standard output for Debug and Info messages and
// standard error for warnings, errors, and fatal messages. // 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 // How do you want to format your log lines? This should be a Go text format
// string, with the following variable placeholders: // string, with the following variable placeholders:

13
go.mod Normal file
View File

@ -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
)

8
go.sum Normal file
View File

@ -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=

View File

@ -32,8 +32,7 @@ func (l *Logger) emit(level logLevel, tmpl string, args ...interface{}) {
// If we have a log writer, send it there. // If we have a log writer, send it there.
if l.Config.Writer != nil { if l.Config.Writer != nil {
// TODO fmt.Fprintln(l.Config.Writer, message)
// l.Config.Writer.Write(message)
} else { } else {
// No writer given so we default to standard out/error. // No writer given so we default to standard out/error.
if level <= InfoLevel { if level <= InfoLevel {

35
tee.go Normal file
View File

@ -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)
}

View File

@ -1,4 +1,6 @@
//go:build js && wasm
// +build js,wasm // +build js,wasm
package log package log
// WASM builds don't have a terminal to query so interactive=false for plain // WASM builds don't have a terminal to query so interactive=false for plain