Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dikkadev/prettyslog
https://github.com/dikkadev/prettyslog
Last synced: 3 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/dikkadev/prettyslog
- Owner: dikkadev
- License: unlicense
- Created: 2024-10-02T18:19:58.000Z (3 months ago)
- Default Branch: main
- Last Pushed: 2024-10-29T12:24:48.000Z (about 2 months ago)
- Last Synced: 2024-12-22T05:10:27.912Z (6 days ago)
- Language: Go
- Size: 8.79 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Prettyslog
`prettyslog` is a custom Go `slog` handler that formats log records according to your preferences. It provides a clean, colored text output with configurable options, ensuring your logs are both informative and easy to read.
## Features
- **Customizable Log Format**: Define how your logs look, including timestamp formats and attribute styling.
- **Color Support**: Enhance readability with color-coded log levels and keys.
- **Group Handling**: Required group names at the start of each log line, with support for nested groups.
- **Functional Options**: Easily configure the handler using functional options.
- **Thread-Safe**: Safe for concurrent use by multiple goroutines.## Installation
```bash
go get github.com/dikkadev/prettyslog
```## Usage
```go
package mainimport (
"log/slog"
"github.com/dikkadev/prettyslog"
)func main() {
// Create a new prettyslog handler with the group "MyApp" and default options
handler := prettyslog.NewPrettyslogHandler("MyApp")// Set the default slog logger to use our custom handler
slog.SetDefault(slog.New(handler))// Now, use slog as usual
slog.Info("Application started", "version", "1.0.0")// Using a logger with additional attributes
logger := slog.With("request_id", "12345")
logger.Info("User logged in", "username", "johndoe")// Using a different group
dbLogger := logger.WithGroup("db")
dbLogger.Info("Query executed", "query", "SELECT * FROM users")// Including source information
handlerWithSource := prettyslog.NewPrettyslogHandler("MyApp",
prettyslog.WithSource(true),
)
slog.SetDefault(slog.New(handlerWithSource))
slog.Info("This log includes source information")
}
```## Configuration Options
`prettyslog` uses functional options for configuration:
- `WithTimestamp(enabled bool)`: Enable or disable timestamps. Default is `true`.
- `WithTimestampFormat(format string)`: Set a custom timestamp format. Default is `"[15:04:05.000]"`.
- `WithSource(enabled bool)`: Include source file and line number. Default is `false`.
- `WithColors(enabled bool)`: Enable or disable colors. Default is `true`.
- `WithLevel(level slog.Leveler)`: Set the minimum log level. Default is `slog.LevelInfo`.
- `WithWriter(w io.Writer)`: Set a custom `io.Writer`. Default is `os.Stderr`.### Example with Custom Options
```go
handler := prettyslog.NewPrettyslogHandler("MyApp",
prettyslog.WithSource(true),
prettyslog.WithLevel(slog.LevelDebug),
prettyslog.WithColors(false),
prettyslog.WithTimestampFormat("[2006-01-02 15:04:05]"),
)
slog.SetDefault(slog.New(handler))
```