GoKit’s logger is zero-config by default — the global instance starts withDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/AndresGT/GoKit/llms.txt
Use this file to discover all available pages before exploring further.
InfoLevel and colored console output the moment you first call any logging function. When you need more control, the Config struct and a handful of package-level functions let you set a custom minimum level, swap in different writers, attach a service name to every entry, and safely initialise (or replace) the global logger from your application’s startup code.
The Config struct
Every logger instance is created from a Config value. All fields are optional; sensible defaults are applied for anything you leave blank.
The minimum severity level an entry must have to be written. Entries below this level are silently dropped. Use
logger.DebugLevel during development and logger.WarnLevel or higher in production.When
true, the built-in ConsoleWriter wraps each line in an ANSI color corresponding to its level. Has no effect on FileWriter or DBWriter.The list of output targets that receive every log entry. If you leave this
nil, GoKit automatically creates a single ConsoleWriter using the value of EnableColors. Provide your own slice to write to files, databases, or any custom destination simultaneously.A free-form string attached to every entry as a
"service" field. Helpful when several microservices write to the same log sink and you need to filter by origin.Creating a logger
logger.New(cfg Config) *Logger
New creates a fresh, independent logger instance. Use it when you need a logger with different settings from the global — for example, a component-scoped logger or a logger created specifically for tests.
cfg.Writers is nil, New automatically adds a ConsoleWriter that respects cfg.EnableColors.
logger.InitGlobal(cfg Config) *Logger
InitGlobal initialises the global logger exactly once, no matter how many goroutines call it concurrently. Subsequent calls are no-ops and return the already-initialised instance. Call it early in main() before spawning any goroutines that might log.
Because
InitGlobal is backed by a sync.Once, it is safe to call from multiple goroutines at startup — only the first call takes effect.Managing the global logger
| Function | Description |
|---|---|
logger.Default() *Logger | Returns the current global logger (lazily created if not yet initialised). |
logger.SetDefault(l *Logger) | Replaces the global logger. Subsequent package-level calls (logger.Info, etc.) use the new instance. |
logger.InitGlobal(cfg Config) *Logger | Initialises the global logger once; returns it. |
Instance methods
These methods operate on a specific*Logger and are safe to call concurrently.
(*Logger).SetMinLevel(level Level)
Adjusts the minimum log level on an existing logger without creating a new instance. Useful for toggling verbosity at runtime based on an environment variable or an admin endpoint.
(*Logger).AddWriter(w Writer)
Appends a new writer to an existing logger. Entries will immediately begin flowing to the new destination.
(*Logger).Close() error
Flushes and closes all writers attached to the logger. Always call Close (or defer log.Close()) before your process exits to ensure queued entries — especially in DBWriter — are fully flushed.
(*Logger).Clone() *Logger
Returns a deep copy of the logger sharing the same writers slice and context fields. The clone and the original are independent — changes to one do not affect the other.
Configuration examples
Environment-based configuration
A common pattern is to select a configuration based on theAPP_ENV environment variable:
Testing with a silent logger
SetMinLevel to OffLevel to suppress all output during unit tests: