From 1c9e62016efa26e53b190f483d14bac73fbba49f Mon Sep 17 00:00:00 2001 From: Benjamin Bengfort Date: Tue, 31 Mar 2026 12:49:51 -0500 Subject: [PATCH] [FIX] Remove out in favor of rlog --- README.md | 1 - out/README.md | 34 ----------- out/badger.go | 25 -------- out/caution.go | 33 ----------- out/out.go | 153 ------------------------------------------------ out/out_test.go | 32 ---------- 6 files changed, 278 deletions(-) delete mode 100644 out/README.md delete mode 100644 out/badger.go delete mode 100644 out/caution.go delete mode 100644 out/out.go delete mode 100644 out/out_test.go diff --git a/README.md b/README.md index 42dab9b..9935815 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,6 @@ This is single repository that stores many, independent small subpackages. This - [humanize](https://go.rtnl.ai/x/humanize): creates human readable strings from various types - [locks](https://go.rtnl.ai/x/locks): key-based lock mechanism to distribute keys across a fixed number of locks - [noplog](https://go.rtnl.ai/x/noplog): no operation logger to capture internal logging with no output -- [out](https://go.rtnl.ai/x/out): hierarchical logger to manage logging verbosity to stdout - [probez](https://go.rtnl.ai/x/probez): http handlers for kubernetes probes (livez, healthz, and readyz) - [query](https://go.rtnl.ai/x/query): encode struct values as a url query string - [radish](https://go.rtnl.ai/x/radish): run asynchronous tasks diff --git a/out/README.md b/out/README.md deleted file mode 100644 index a9398db..0000000 --- a/out/README.md +++ /dev/null @@ -1,34 +0,0 @@ -# Out - -**Package out implements simple hierarchical logging functionality.** - -This is a pretty standard hierarchical output logging module that allows you to use tiered functions to manage what is printed to the command line. For example if the level is set to `Info`, then `Trace` and `Debug` messages will be implemented as no-ops, reducing the amount of information printed. - -This package also provides a Caution log level - caution messages are only printed if -a specific threshold of messages has been reached. This helps to reduce the number of -repeated messages (e.g. connection down) that occur in logging while still giving -effective debugging and systems administration feedback to the user. - -Setting up the console usually happens in the `init()` method of a package: - -```go -func init() { - // Initialize our debug logging with our prefix - out.Init("[myapp] ", log.Lmicroseconds) - out.SetLevel(out.LevelInfo) -} -``` - -Now the logging functions can automatically be used: - -```go -out.Trace("routine %s happening", thing) -out.Debug("sending message #%d from %s to %s", msg, send, recv) -out.Info("listening on %s", addr) -out.Caution("could not reach %s -- connection is down", addr) -out.Status("completed %d out of %d tasks", completed, nTasks) -out.Warn("limit of %d queries reached", nQueries) -out.Warne(err) -``` - -The purpose of these functions were to have simple `pout` and `perr` methods inside of applications. diff --git a/out/badger.go b/out/badger.go deleted file mode 100644 index c04dfd2..0000000 --- a/out/badger.go +++ /dev/null @@ -1,25 +0,0 @@ -package out - -// BadgerLogger implements the badger.Logger to ensure database log messages get -// appropriately logged at the level specified (and use the internal logger). -type BadgerLogger struct{} - -// Errorf writes a log message to out.Warn -func (l *BadgerLogger) Errorf(msg string, a ...interface{}) { - Warn(msg, a...) -} - -// Warningf writes a log message to out.Caution -func (l *BadgerLogger) Warningf(msg string, a ...interface{}) { - Caution(msg, a...) -} - -// Infof writes a message to out.Info -func (l *BadgerLogger) Infof(msg string, a ...interface{}) { - Info(msg, a...) -} - -// Debugf writes a message to out.Debug -func (l *BadgerLogger) Debugf(msg string, a ...interface{}) { - Debug(msg, a...) -} diff --git a/out/caution.go b/out/caution.go deleted file mode 100644 index 505724c..0000000 --- a/out/caution.go +++ /dev/null @@ -1,33 +0,0 @@ -package out - -import "sync" - -// Counts the number of caution messages until a threshold is reached -type counter struct { - sync.Mutex - counts map[string]uint - threshold uint -} - -// initialize the counter object -func (c *counter) init() { - c.counts = make(map[string]uint) -} - -// keep track of the number of messages logged, if seen for the first time, return true, -// otherwise if greater than the threshold, remove it so the next time the message is -// observed it returns true. -func (c *counter) log(msg string) bool { - c.Lock() - defer c.Unlock() - - c.counts[msg]++ - if c.counts[msg] == 1 { - return true - } - - if c.counts[msg] > c.threshold-1 { - delete(c.counts, msg) - } - return false -} diff --git a/out/out.go b/out/out.go deleted file mode 100644 index 180d016..0000000 --- a/out/out.go +++ /dev/null @@ -1,153 +0,0 @@ -/* -Package out implements simple hierarchical logging functionality for debugging and -logging. The package can write to any configured logger, but generally writes to stdout -(hence the name of the package) for use with system logging. The log level specifies the -verbosity of the output. For example if the level is set to Info, then Debug and Trace -messages will become no-ops and ignored by the logger. - -This package also provides a Caution log level - caution messages are only printed if -a specific threshold of messages has been reached. This helps to reduce the number of -repeated messages (e.g. connection down) that occur in logging while still giving -effective debugging and systems administration feedback to the user. -*/ -package out - -import ( - "fmt" - "log" - "os" - "strings" -) - -func init() { - logger = log.New(os.Stdout, "", 0) - cautionCounter = new(counter) - cautionCounter.init() -} - -// Logging levels for specify the verbosity of log output. The higher the level, the -// less verbose the output is. The out will log messages >= to the specified level. -const ( - LevelTrace uint8 = iota - LevelDebug - LevelInfo - LevelCaution - LevelStatus - LevelWarn - LevelSilent -) - -// DefaultCautionThreshold for issuing caution output to the logger after accumulating messages. -const DefaultCautionThreshold = 80 - -// This package acts as a module level logger, so the variables are all at the top level. -var ( - logLevel = LevelInfo - logger *log.Logger - cautionCounter *counter - logLevelStrings = [...]string{ - "trace", "debug", "info", "status", "warn", "silent", - } -) - -//=========================================================================== -// Interact with debug output -//=========================================================================== - -// Init the output logger to stdout with the prefix and log options -func Init(prefix string, flag int) { - logger = log.New(os.Stdout, prefix, flag) -} - -// LogLevel returns a string representation of the current level. -func LogLevel() string { - return logLevelStrings[logLevel] -} - -// SetLogLevel modifies the log level for messages at runtime. Ensures that -// the highest level that can be set is the trace level. This function is -// often called from outside of the package in an init() function to define -// how logging is handled in the console. -func SetLogLevel(level uint8) { - if level > LevelSilent { - level = LevelSilent - } - - logLevel = level -} - -// SetLogger to write output to. -func SetLogger(lg *log.Logger) { - logger = lg -} - -// SetCautionThreshold to the specified number of messages before print. -func SetCautionThreshold(threshold uint) { - cautionCounter.threshold = threshold -} - -//=========================================================================== -// Debugging output functions -//=========================================================================== - -// Print to the standard logger at the specified level. Arguments are handled -// in the manner of log.Printf, but a newline is appended. -func print(level uint8, msg string, a ...interface{}) { - if logLevel <= level { - if !strings.HasSuffix(msg, "\n") { - msg += "\n" - } - logger.Printf(msg, a...) - } -} - -// Warn prints to the standard logger if level is warn or greater; arguments -// are handled in the manner of log.Printf, but a newline is appended. -func Warn(msg string, a ...interface{}) { - print(LevelWarn, msg, a...) -} - -// Warne is a helper function to simply warn about an error received. -func Warne(err error) { - Warn(err.Error()) -} - -// Status prints to the standard logger if level is status or greater; -// arguments are handled in the manner of log.Printf, but a newline is appended. -func Status(msg string, a ...interface{}) { - print(LevelStatus, msg, a...) -} - -// Caution prints to the standard logger if the level is caution or greater and if the -// number of times caution has been called with the same message has reached the -// threshold. This reduces the number of repeated log output messages while still -// allowing the system to report valuable information. -func Caution(msg string, a ...interface{}) { - if logLevel > LevelCaution { - // Don't waste memory if the log level is set above caution. - return - } - - msg = fmt.Sprintf(msg, a...) - if cautionCounter.log(msg) { - print(LevelCaution, msg) - } -} - -// Info prints to the standard logger if level is info or greater; arguments -// are handled in the manner of log.Printf, but a newline is appended. -func Info(msg string, a ...interface{}) { - print(LevelInfo, msg, a...) -} - -// Debug prints to the standard logger if level is debug or greater; -// arguments are handled in the manner of log.Printf, but a newline is appended. -func Debug(msg string, a ...interface{}) { - print(LevelDebug, msg, a...) -} - -// Trace prints to the standard logger if level is trace or greater; -// arguments are handled in the manner of log.Printf, but a newline is appended. -func Trace(msg string, a ...interface{}) { - print(LevelTrace, msg, a...) -} diff --git a/out/out_test.go b/out/out_test.go deleted file mode 100644 index 4ba34cc..0000000 --- a/out/out_test.go +++ /dev/null @@ -1,32 +0,0 @@ -package out_test - -import ( - "errors" - - "go.rtnl.ai/x/out" -) - -func Example() { - out.Init("[test] ", 0) - out.SetLogLevel(out.LevelInfo) - out.SetCautionThreshold(2) - - out.Trace("routine %s happening", "thing") - out.Debug("sending message #%d from %s to %s", 42, "me", "you") - out.Info("listening on %s", "127.0.0.1") - out.Caution("could not reach %s -- connection is down", "uptime.robot") - out.Status("completed %d out of %d tasks", 42, 121) - out.Warn("limit of %d queries reached", 21) - out.Warne(errors.New("something bad happened")) - - out.Caution("could not reach %s -- connection is down", "uptime.robot") - out.Caution("could not reach %s -- connection is down", "uptime.robot") - - // Output: - // [test] listening on 127.0.0.1 - // [test] could not reach uptime.robot -- connection is down - // [test] completed 42 out of 121 tasks - // [test] limit of 21 queries reached - // [test] something bad happened - // [test] could not reach uptime.robot -- connection is down -}