Documentation
¶
Overview ¶
Example ¶
package main
import (
"errors"
"time"
"github.com/ayankit/clog"
)
func main() {
clog.Init(clog.LevelDebug)
clog.Info("Starting server", "addr", ":8080", "env", "production")
clog.Debug("Connected to DB", "db", "myapp", "host", "localhost:5432")
clog.Warn("Slow request", "method", "GET", "path", "/users", "duration", 497*time.Millisecond)
clog.Error("DB connection lost", clog.Err(errors.New("connection reset")), "db", "myapp")
}
Example (WithLogToFile) ¶
Create a new logger that writes to given log file
package main
import (
"errors"
"time"
"github.com/ayankit/clog"
)
func main() {
clog.Init(clog.LevelDebug, "/path/to/log/file")
clog.Info("Starting server", "addr", ":8080", "env", "production")
clog.Debug("Connected to DB", "db", "myapp", "host", "localhost:5432")
clog.Warn("Slow request", "method", "GET", "path", "/users", "duration", 497*time.Millisecond)
clog.Error("DB connection lost", clog.Err(errors.New("connection reset")), "db", "myapp")
}
Index ¶
- func Attr(color uint8, attr slog.Attr) slog.Attr
- func Debug(msg string, args ...any)
- func Debugf(format string, args ...any)
- func Err(err error) slog.Attr
- func Error(msg string, args ...any)
- func Errorf(format string, args ...any)
- func Fatal(msg string, args ...any)
- func Fatalf(format string, args ...any)
- func Info(msg string, args ...any)
- func Infof(format string, args ...any)
- func Init(level Level, logFilePath ...string) error
- func NewHandler(w io.Writer, opts *Options) slog.Handler
- func Warn(msg string, args ...any)
- func Warnf(format string, args ...any)
- func With(args ...any) *slog.Logger
- type Level
- type Options
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Attr ¶
Attr returns a tinted (colorized) slog.Attr that will be written in the specified color by the [tint.Handler]. When used with any other slog.Handler, it behaves as a plain slog.Attr.
Use the uint8 color value to specify the color of the attribute:
- 0-7: standard ANSI colors
- 8-15: high intensity ANSI colors
- 16-231: 216 colors (6×6×6 cube)
- 232-255: grayscale from dark to light in 24 steps
func Err ¶
Err returns a tinted (colorized) slog.Attr that will be written in red color by the [tint.Handler]. When used with any other slog.Handler, it behaves as
slog.Any("err", err)
func NewHandler ¶
NewHandler creates a slog.Handler that writes tinted logs to Writer w, using the default options. If opts is nil, the default options are used.
Types ¶
type Options ¶
type Options struct {
// Enable source code location (Default: false)
AddSource bool
// Minimum level to log (Default: slog.LevelInfo)
Level slog.Leveler
// ReplaceAttr is called to rewrite each non-group attribute before it is logged.
// See https://pkg.go.dev/log/slog#HandlerOptions for details.
ReplaceAttr func(groups []string, attr slog.Attr) slog.Attr
// Time format (Default: time.StampMilli)
TimeFormat string
// Disable color (Default: false)
NoColor bool
}
Options for a slog.Handler that writes tinted logs. A zero Options consists entirely of default values.
Options can be used as a drop-in replacement for slog.HandlerOptions.