Fixed lint issues and missing documentation
This commit is contained in:
1 parent
7fd321df97
commit
6e6dec68c0
34 files changed
+346
-156
No files matched your search
+20
-10
@@ -1,23 +1,33 @@
|
||||
package logging
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"log/slog"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/lmittmann/tint"
|
||||
)
|
||||
|
||||
// ---------- Standard log handling (terminal or file based) ----------
|
||||
|
||||
var logLevel slog.LevelVar
|
||||
var fileLogger *os.File
|
||||
// Logger state shared by the terminal and file loggers.
|
||||
var (
|
||||
// logLevel is the minimum level logged, read by whichever handler is
|
||||
// installed, so changing it takes effect immediately.
|
||||
logLevel slog.LevelVar
|
||||
// fileLogger is the log file opened by LogToFile, or nil when logging
|
||||
// to the terminal.
|
||||
fileLogger *os.File
|
||||
)
|
||||
|
||||
// Set the logging to a particular level, quietly
|
||||
// SetLevel sets the logging level without logging the change.
|
||||
func SetLevel(level slog.Level) {
|
||||
logLevel.Set(level)
|
||||
}
|
||||
|
||||
// Enable debug level logging
|
||||
// EnableDebug lowers the logging level to debug, if it isn't already, and
|
||||
// logs that it did.
|
||||
func EnableDebug() {
|
||||
if logLevel.Level() > slog.LevelDebug {
|
||||
logLevel.Set(slog.LevelDebug)
|
||||
@@ -29,15 +39,15 @@ func EnableDebug() {
|
||||
// logger. The file is created if needed and appended to; any log file opened
|
||||
// by an earlier call is closed.
|
||||
func LogToFile(logfile string) error {
|
||||
if fileLogger != nil {
|
||||
(*fileLogger).Close()
|
||||
}
|
||||
closeErr := Shutdown()
|
||||
|
||||
// Open the log file
|
||||
var err error
|
||||
fileLogger, err = os.OpenFile(logfile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0o600)
|
||||
fileLogger, err = os.OpenFile(
|
||||
filepath.Clean(logfile), os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0o600,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
return errors.Join(err, closeErr)
|
||||
}
|
||||
|
||||
opts := &slog.HandlerOptions{
|
||||
@@ -47,7 +57,7 @@ func LogToFile(logfile string) error {
|
||||
logger := slog.New(handler)
|
||||
slog.SetDefault(logger)
|
||||
|
||||
return nil
|
||||
return closeErr
|
||||
}
|
||||
|
||||
// Shutdown closes the log file opened by LogToFile, if any.
|
||||
|
||||
Reference in new issue
Block a user