-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from simplifi/global-logger
Global logger
- Loading branch information
Showing
9 changed files
with
147 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package logger | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/charmbracelet/log" | ||
) | ||
|
||
// Global logger instance | ||
var Log *log.Logger | ||
|
||
const ( | ||
// DefaultLogLevel is the default log level for the logger | ||
DefaultLogLevel = log.InfoLevel | ||
) | ||
|
||
// init initializes the global logger instance. It sets the output to stdout | ||
// and the log level to the DefaultLogLevel. | ||
func init() { | ||
Log = log.New(os.Stdout) | ||
Log.SetLevel(DefaultLogLevel) | ||
} | ||
|
||
// SetLevel sets the logging level for the global logger. | ||
// It accepts a string representation of the log level, | ||
// such as "debug", "info", "warn", "error", or "fatal". | ||
// If an invalid level is provided, it defaults to the DefaultLogLevel | ||
// and logs a warning message. | ||
func SetLevel(level string) { | ||
// Attempt to parse the provided log level string into a log.Level value. | ||
lvl, err := log.ParseLevel(level) | ||
|
||
// If the parsing fails, it indicates an invalid log level was provided. | ||
if err != nil { | ||
Log.Warn("Invalid log level provided, using default instead", | ||
"level", level, | ||
"default", DefaultLogLevel, | ||
"error", err) | ||
|
||
// Set the log level to the default level. | ||
lvl = DefaultLogLevel | ||
} | ||
|
||
// Set the log level of the global logger to the determined level. | ||
Log.SetLevel(lvl) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.