REPOSITORY / ScuroNeko/SNekLog

Compare commits

DIFF REPOSITORY
6 Commits
Author SHA1 Message Date
ScuroNeko 3eac1851ec (new): prepare v2.1.0 release
Golang lint / lint (push) Successful in 24s
- add custom LogLevel constructors and richer color configuration
- support ANSI, 256-color, RGB, and text attributes on LogLevel
- introduce clearer preferred APIs such as NewLogger and SetName
- preserve backward compatibility with deprecated wrappers for v2.0.1 APIs
- restore legacy ColorStringBuilder signatures as compatibility wrappers
- fix newline separator handling in Println-style output
- add tests for color precedence, deprecated aliases, and LogLevel attributes
- update GoDoc and bilingual README documentation
2026-04-27 15:42:24 +03:00
ScuroNeko b4c9203a79 (fix): lint error
Golang lint / lint (push) Successful in 50s
2026-04-27 10:24:53 +03:00
ScuroNeko 19f8750d35 (fix): module declaration slog->sneklog
Golang lint / lint (push) Failing after 48s
2026-04-27 09:56:46 +03:00
ScuroNeko d534f92d48 (refactor): prepare breaking v2 API
- rename package to sneklog and move module to /v2
- replace text output flags with writer formatters
- remove external color dependencies
- update migration notes and coverage
2026-04-24 15:43:14 +03:00
ScuroNeko 14b787710c Merge branch 'main' of scuroneko.dev:ScuroNeko/slog 2026-04-20 18:11:30 +03:00
ScuroNeko 9268e222ab feat: add message replacement for log records
Add Logger.AddReplacer to mask or normalize message text before records reach writers.
Document replacement behavior in README and package GoDoc, and update the example.

Also fix JSON writer edge cases:
- avoid panic on empty message lists
- preserve newline semantics when the last message already ends with n
- keep original message argument types for custom writers when no replacers are configured

Update dependencies and add release notes for the next release.
2026-04-20 18:00:11 +03:00
18 changed files with 1642 additions and 208 deletions
+12
View File
@@ -0,0 +1,12 @@
name: Golang lint
run-name: Linting code
on: [push]
jobs:
lint:
runs-on: go-latest
steps:
- name: Checkout repository code
uses: actions/checkout@v6
- name: Run golangci-lint
run: golangci-lint run
+2
View File
@@ -1,3 +1,5 @@
.idea/ .idea/
.vscode/
.codex
test/ test/
*.log *.log
+54 -16
View File
@@ -1,4 +1,4 @@
# slog # SNekLog (ScuroNeko Logger)
Small structured logger for Go with text and JSON output, multiple writers, and optional traceback metadata. Small structured logger for Go with text and JSON output, multiple writers, and optional traceback metadata.
@@ -11,12 +11,13 @@ Russian version: [README_ru.md](README_ru.md)
- `stdout`, files, and arbitrary external `io.Writer` values. - `stdout`, files, and arbitrary external `io.Writer` values.
- Optional timestamps for text output. - Optional timestamps for text output.
- Compact traceback metadata for text writers and full traceback slices for JSON. - Compact traceback metadata for text writers and full traceback slices for JSON.
- Message replacement for masking secrets or normalizing output.
- Explicit ownership rules for writer closing. - Explicit ownership rules for writer closing.
## Installation ## Installation
```bash ```bash
go get git.scuroneko.dev/scuroneko/slog go get git.scuroneko.dev/scuroneko/sneklog/v2
``` ```
## Quick start ## Quick start
@@ -27,15 +28,14 @@ package main
import ( import (
"log" "log"
"git.scuroneko.dev/scuroneko/slog" "git.scuroneko.dev/scuroneko/sneklog/v2"
) )
func main() { func main() {
logger := slog.CreateLogger(). logger := sneklog.NewLogger().
Prefix("API"). SetName("API").
Level(slog.DEBUG). SetLevel(sneklog.DEBUG).
PrintTraceback(true). AddReplacer("SOME_SECRET", "<redacted>")
JsonPretty(true)
text := logger.CreateTextStdoutWriter() text := logger.CreateTextStdoutWriter()
jsonFile, err := logger.CreateJsonFileWriter("logs/app.json") jsonFile, err := logger.CreateJsonFileWriter("logs/app.json")
@@ -49,6 +49,7 @@ func main() {
logger.Warnln("cache miss") logger.Warnln("cache miss")
logger.Errorln("request failed") logger.Errorln("request failed")
logger.Debugln("debug details") logger.Debugln("debug details")
logger.Infoln("token", "SOME_SECRET")
if err := logger.Close(); err != nil { if err := logger.Close(); err != nil {
log.Fatal(err) log.Fatal(err)
@@ -58,15 +59,17 @@ func main() {
## Defaults ## Defaults
`CreateLogger()` starts with: `NewLogger()` starts with:
- `Prefix("LOG")` - `Prefix("LOG")`
- `Level(slog.FATAL)` - `Level(sneklog.FATAL)`
- `PrintTime(true)`
- `PrintTraceback(false)`
- `JsonPretty(false)` - `JsonPretty(false)`
- text formatter: `sneklog.DefaultTextFormatter`
- JSON formatter: `sneklog.DefaultJsonFormatter`
Important: with the current level ordering, `Level(slog.FATAL)` allows `INFO`, `WARN`, `ERROR`, and `FATAL`, but not `DEBUG`. Use `Level(slog.DEBUG)` to enable every level. `CreateLogger()` is still available for backward compatibility.
Important: with the current level ordering, `Level(sneklog.FATAL)` allows `INFO`, `WARN`, `ERROR`, and `FATAL`, but not `DEBUG`. Use `Level(sneklog.DEBUG)` to enable every level.
## Writers and ownership ## Writers and ownership
@@ -89,10 +92,10 @@ This makes it safe to plug in `bytes.Buffer`, network writers, and other externa
Text writers render records like: Text writers render records like:
```text ```text
[API] [INFO] [main.go:main:27] [17.03.26 14:05:09] service started 2026-03-17T14:05:09+03:00 info API: service started
``` ```
The traceback field is included only when `PrintTraceback(true)` is enabled. Use `SetFormatter` on a writer to customize timestamps, traceback fields, colors, and message layout.
JSON writers emit objects with this shape: JSON writers emit objects with this shape:
@@ -116,18 +119,53 @@ JSON writers emit objects with this shape:
When `JsonPretty(true)` is enabled, JSON is indented. When `JsonPretty(true)` is enabled, JSON is indented.
## Custom levels and colors
You can define your own levels and assign ANSI, 256-color, or RGB colors, plus text attributes such as `Bold` or `Italic`.
```go
httpDelete := sneklog.NewLogLevel(0, "delete")
httpDelete.SetBackgroundRGB(128, 0, 0)
httpDelete.AddAttribute(sneklog.Italic).AddAttribute(sneklog.Bold)
httpCache := sneklog.NewLogLevel(0, "cache")
httpCache.SetForeground256Color(214)
```
Because `LogLevel` setters mutate the level in place, call them on a variable, not on a temporary value returned by `NewLogLevel(...)`.
Short forms such as `SetFgColor` and `SetBgColor` remain available for backward compatibility.
## Message replacement
`AddReplacer(old, new)` replaces matching text in every message before the
record reaches any writer. Replacement rules are applied in the order they are
added.
```go
logger := sneklog.CreateLogger().
Level(sneklog.DEBUG).
AddReplacer("SOME_SECRET", "<redacted>").
AddReplacer("user@example.com", "<email>")
logger.Infoln("login token:", "SOME_SECRET")
```
This writes `<redacted>` instead of `SOME_SECRET` in both text and JSON output.
An empty `old` value is ignored.
## API summary ## API summary
- `Info`, `Warn`, `Error`, `Debug`, and `Fatal` accept a list of values. - `Info`, `Warn`, `Error`, `Debug`, and `Fatal` accept a list of values.
- `Infof`, `Warnf`, `Errorf`, `Debugf`, and `Fatalf` use `fmt.Sprintf`. - `Infof`, `Warnf`, `Errorf`, `Debugf`, and `Fatalf` use `fmt.Sprintf`.
- The `*ln` methods preserve newline semantics, which is useful for `stdout`, Docker, and line-based collectors. - The `*ln` methods preserve newline semantics, which is useful for `stdout`, Docker, and line-based collectors.
- `Fatal`, `Fatalf`, and `Fatalln` call `os.Exit(1)` after writing the message. - `Fatal`, `Fatalf`, and `Fatalln` call `os.Exit(1)` after writing the message.
- `AddReplacer` masks or rewrites message text before records are sent to writers.
## Traceback behavior ## Traceback behavior
- Text writers use the nearest user stack frame. - Text writers use the nearest user stack frame.
- JSON writers receive the full traceback slice. - JSON writers receive the full traceback slice.
- Internal `slog` frames and `runtime` frames are filtered out. - Internal `sneklog` frames and `runtime` frames are filtered out.
## Repository example ## Repository example
+53 -16
View File
@@ -1,4 +1,4 @@
# slog # SNekLog (ScuroNeko Logger)
Небольшой структурированный логгер для Go с текстовым и JSON-выводом, несколькими writer'ами и настраиваемыми traceback-метаданными. Небольшой структурированный логгер для Go с текстовым и JSON-выводом, несколькими writer'ами и настраиваемыми traceback-метаданными.
@@ -11,12 +11,13 @@ English version: [README.md](README.md)
- `stdout`, файлы и любые внешние `io.Writer`. - `stdout`, файлы и любые внешние `io.Writer`.
- Опциональные timestamp'ы для текстового вывода. - Опциональные timestamp'ы для текстового вывода.
- Компактный traceback для текстовых writer'ов и полный traceback для JSON. - Компактный traceback для текстовых writer'ов и полный traceback для JSON.
- Замена текста в сообщениях для маскирования секретов или нормализации вывода.
- Явные правила владения writer'ами при `Close()`. - Явные правила владения writer'ами при `Close()`.
## Установка ## Установка
```bash ```bash
go get git.scuroneko.dev/scuroneko/slog go get git.scuroneko.dev/scuroneko/sneklog/v2
``` ```
## Быстрый старт ## Быстрый старт
@@ -27,15 +28,14 @@ package main
import ( import (
"log" "log"
"git.scuroneko.dev/scuroneko/slog" "git.scuroneko.dev/scuroneko/sneklog/v2"
) )
func main() { func main() {
logger := slog.CreateLogger(). logger := sneklog.NewLogger().
Prefix("API"). SetName("API").
Level(slog.DEBUG). SetLevel(sneklog.DEBUG).
PrintTraceback(true). AddReplacer("SOME_SECRET", "<redacted>")
JsonPretty(true)
text := logger.CreateTextStdoutWriter() text := logger.CreateTextStdoutWriter()
jsonFile, err := logger.CreateJsonFileWriter("logs/app.json") jsonFile, err := logger.CreateJsonFileWriter("logs/app.json")
@@ -49,6 +49,7 @@ func main() {
logger.Warnln("cache miss") logger.Warnln("cache miss")
logger.Errorln("request failed") logger.Errorln("request failed")
logger.Debugln("debug details") logger.Debugln("debug details")
logger.Infoln("token", "SOME_SECRET")
if err := logger.Close(); err != nil { if err := logger.Close(); err != nil {
log.Fatal(err) log.Fatal(err)
@@ -58,15 +59,17 @@ func main() {
## Значения по умолчанию ## Значения по умолчанию
`CreateLogger()` создает логгер со следующими настройками: `NewLogger()` создает логгер со следующими настройками:
- `Prefix("LOG")` - `Prefix("LOG")`
- `Level(slog.FATAL)` - `Level(sneklog.FATAL)`
- `PrintTime(true)`
- `PrintTraceback(false)`
- `JsonPretty(false)` - `JsonPretty(false)`
- текстовый formatter: `sneklog.DefaultTextFormatter`
- JSON formatter: `sneklog.DefaultJsonFormatter`
Важно: в текущей модели уровней `Level(slog.FATAL)` пропускает `INFO`, `WARN`, `ERROR` и `FATAL`, но не `DEBUG`. Чтобы включить все сообщения, используйте `Level(slog.DEBUG)`. `CreateLogger()` по-прежнему доступен для обратной совместимости.
Важно: в текущей модели уровней `Level(sneklog.FATAL)` пропускает `INFO`, `WARN`, `ERROR` и `FATAL`, но не `DEBUG`. Чтобы включить все сообщения, используйте `Level(sneklog.DEBUG)`.
## Writer'ы и владение ## Writer'ы и владение
@@ -89,10 +92,10 @@ func main() {
Текстовый writer формирует записи вида: Текстовый writer формирует записи вида:
```text ```text
[API] [INFO] [main.go:main:27] [17.03.26 14:05:09] service started 2026-03-17T14:05:09+03:00 info API: service started
``` ```
Поле traceback появляется только если включен `PrintTraceback(true)`. Используйте `SetFormatter` у writer'а, чтобы настроить timestamp'ы, traceback-поля, цвета и шаблон сообщения.
JSON writer записывает объект со следующими полями: JSON writer записывает объект со следующими полями:
@@ -116,18 +119,52 @@ JSON writer записывает объект со следующими поля
Если включен `JsonPretty(true)`, JSON выводится с отступами. Если включен `JsonPretty(true)`, JSON выводится с отступами.
## Кастомные уровни и цвета
Можно создавать собственные уровни и назначать им ANSI, 256-color или RGB-цвета, а также текстовые атрибуты вроде `Bold` и `Italic`.
```go
httpDelete := sneklog.NewLogLevel(0, "delete")
httpDelete.SetBackgroundRGB(128, 0, 0)
httpDelete.AddAttribute(sneklog.Italic).AddAttribute(sneklog.Bold)
httpCache := sneklog.NewLogLevel(0, "cache")
httpCache.SetForeground256Color(214)
```
Так как setter'ы `LogLevel` изменяют уровень на месте, их нужно вызывать на переменной, а не на временном результате `NewLogLevel(...)`.
Короткие формы вроде `SetFgColor` и `SetBgColor` сохранены для обратной совместимости.
## Замена сообщений
`AddReplacer(old, new)` заменяет найденный текст в каждом сообщении до того,
как запись попадет в writer'ы. Правила замены применяются в порядке добавления.
```go
logger := sneklog.CreateLogger().
Level(sneklog.DEBUG).
AddReplacer("SOME_SECRET", "<redacted>").
AddReplacer("user@example.com", "<email>")
logger.Infoln("login token:", "SOME_SECRET")
```
В текстовом и JSON-выводе вместо `SOME_SECRET` будет записано `<redacted>`.
Пустое значение `old` игнорируется.
## API кратко ## API кратко
- `Info`, `Warn`, `Error`, `Debug`, `Fatal` принимают список значений. - `Info`, `Warn`, `Error`, `Debug`, `Fatal` принимают список значений.
- `Infof`, `Warnf`, `Errorf`, `Debugf`, `Fatalf` используют `fmt.Sprintf`. - `Infof`, `Warnf`, `Errorf`, `Debugf`, `Fatalf` используют `fmt.Sprintf`.
- Методы `*ln` добавляют семантику перевода строки, что удобно для `stdout`, Docker и line-based collectors. - Методы `*ln` добавляют семантику перевода строки, что удобно для `stdout`, Docker и line-based collectors.
- `Fatal`, `Fatalf` и `Fatalln` вызывают `os.Exit(1)` после записи сообщения. - `Fatal`, `Fatalf` и `Fatalln` вызывают `os.Exit(1)` после записи сообщения.
- `AddReplacer` маскирует или переписывает текст сообщений перед отправкой в writer'ы.
## Поведение traceback ## Поведение traceback
- Текстовые writer'ы используют ближайший пользовательский stack frame. - Текстовые writer'ы используют ближайший пользовательский stack frame.
- JSON writer'ы получают полный traceback. - JSON writer'ы получают полный traceback.
- Внутренние frame'ы `slog` и `runtime` фильтруются из traceback. - Внутренние frame'ы `sneklog` и `runtime` фильтруются из traceback.
## Пример из репозитория ## Пример из репозитория
+149
View File
@@ -0,0 +1,149 @@
# v2.1.0
This release expands sneklog color customization while preserving compatibility with the v2.0.1 API.
## Added
- custom LogLevel constructors with configurable colors
- 256-color and RGB support for LogLevel
- ANSI text attributes on LogLevel such as Bold and Italic
- typed color helpers for foreground/background RGB and 256-color values
- NewLogger, SetName, SetLevel, and SetJSONPretty as clearer preferred APIs
## Improved
- text and JSON writer handling of trailing newline semantics
- formatter colorization logic for foreground, background, and attributes
- GoDoc coverage across the public API
- English and Russian README examples and API guidance
## Compatibility
- legacy APIs from v2.0.1 remain available as deprecated wrappers
- legacy ColorStringBuilder method signatures were preserved for backward compatibility
- deprecated methods now explicitly document their replacements and planned removal in v3
## Fixed
- extra separator/space issues in Println-style output
- attribute slice handling and defensive copying for LogLevel
- color mode precedence when switching between ANSI, 256-color, and RGB modes
# v2.0.0
## Highlights
- Renamed the public Go package from `slog` to `sneklog` to avoid confusion with the standard library `log/slog` package.
- Moved the module path to `git.scuroneko.dev/scuroneko/sneklog/v2` for Go module major-version compatibility.
- Replaced fixed text writer settings with formatter-based output customization.
- Added built-in ANSI color support and removed the external `github.com/fatih/color` dependency tree.
- Formatters: a new way do display your logs!
## Breaking Changes
- Import paths must use the `/v2` module suffix:
```go
import "git.scuroneko.dev/scuroneko/sneklog/v2"
```
- Package references should use `sneklog` instead of `slog`.
- Removed `Logger.PrintTime` and `Logger.PrintTraceback`; configure output through writer formatters instead.
- Changed text writer constructors:
- `CreateTextWriter(w, printTraceback, printTime)` is now `CreateTextWriter(w)`.
- `CreateTextFileWriter(path, printTraceback, printTime)` is now `CreateTextFileWriter(path)`.
- `CreateTextStdoutWriter(printTraceback, printTime)` is now `CreateTextStdoutWriter()`.
- Removed the old text formatting helpers: `BuildString`, `FormatTime`, `FormatTraceback`, and `FormatFullTraceback`.
- JSON `time` values are now formatted through the configured formatter and stored as strings in `LoggerJsonMessage`.
## Added
- `Formatter` for configurable message layouts, timestamp layouts, traceback layouts, separators, and color behavior.
- `DefaultTextFormatter` and `DefaultJsonFormatter` defaults for text and JSON writers.
- `LoggerTextWriter.SetFormatter` and `LoggerJsonWriter.SetFormatter`.
- Strftime-like timestamp format constants such as `RFC3339`, `Kitchen`, `DateTime`, `DateOnly`, and `TimeOnly`.
- Built-in color types and helpers:
- `FgColor`, `BgColor`, and `Attribute`.
- `ColorStringBuilder`.
- Level color getters/setters on `LogLevel`.
- Tests covering formatter defaults, empty traceback placeholders, literal `%` sequences in messages, color controls, and JSON newline semantics.
## Changed
- Text output defaults to `DefaultTextFormatter` instead of the old hard-coded string builder.
- JSON output defaults to `DefaultJsonFormatter` and can now format the JSON `message` field through `Formatter`.
- Newline semantics are normalized before text and JSON formatting, so trailing `\n` controls record termination without becoming part of the message.
- Text color output is enabled by default only for stdout/stderr.
- Example code now demonstrates custom formatters and color configuration.
- Go dependencies were simplified by removing:
- `github.com/fatih/color`
- `github.com/mattn/go-colorable`
- `github.com/mattn/go-isatty`
- `golang.org/x/sys`
## Fixed
- `NewFormatter()` now returns a fresh copy instead of mutating `DefaultTextFormatter`.
- `Formatter.FormatMessage` no longer panics when called with an empty traceback.
- Placeholder-looking text inside log messages, such as `%L` or `%s`, is preserved literally.
- `SetColorOutput(false)` disables color output.
- `SetColorOnlyStdout(false)` allows color output for non-stdout writers.
## Migration
Before:
```go
import "git.scuroneko.dev/scuroneko/slog"
logger := slog.CreateLogger().
Prefix("API").
Level(slog.DEBUG).
PrintTraceback(true)
text := logger.CreateTextStdoutWriter()
```
After:
```go
import sneklog "git.scuroneko.dev/scuroneko/sneklog/v2"
logger := sneklog.CreateLogger().
Prefix("API").
Level(sneklog.DEBUG)
formatter := sneklog.NewFormatter().
SetFormat("%t [%L] %N: %m (%B)")
text := logger.CreateTextStdoutWriter().
SetFormatter(formatter)
```
# v1.2.0
## Highlights
- Added message replacement with `Logger.AddReplacer(old, new)` for masking secrets or normalizing log output.
- Preserved the existing `LoggerWriter.Print(...any)` contract, so custom writers do not need a signature update.
- Improved JSON writer newline handling for messages that already end with `\n`.
## Added
- `Logger.AddReplacer(old, new)` appends a replacement rule that is applied before records are sent to writers.
- README and package GoDoc now document message replacement and include usage examples.
- Tests covering message type preservation for custom writers, JSON empty messages, and JSON trailing newline behavior.
## Fixed
- `LoggerJsonWriter.Print` no longer panics when called with no message arguments.
- `LoggerJsonWriter.Print` now treats a trailing newline at the end of the last message as newline semantics and does not include that newline in the JSON `message` field.
- Log messages keep their original argument types for custom writers when no replacement rules are configured.
## Changed
- Updated the example program to demonstrate `AddReplacer`.
- Updated dependencies:
- `github.com/fatih/color` from `v1.18.0` to `v1.19.0`
- `github.com/mattn/go-isatty` from `v0.0.20` to `v0.0.21`
- `golang.org/x/sys` from `v0.42.0` to `v0.43.0`
## Compatibility
No breaking API changes are intended in this release. The public `LoggerWriter` interface continues to accept `messages ...any`.
+276
View File
@@ -0,0 +1,276 @@
package sneklog
import (
"fmt"
"strings"
)
type FgColor uint8
type BgColor uint8
type FgColor256 uint8
type BgColor256 uint8
type BgColorRGB []uint8
type FgColorRGB []uint8
type Attribute uint8
// NewBgColorRGB builds an RGB background color value.
func NewBgColorRGB(r, g, b uint8) BgColorRGB {
return BgColorRGB{r, g, b}
}
// NewFgColorRGB builds an RGB foreground color value.
func NewFgColorRGB(r, g, b uint8) FgColorRGB {
return FgColorRGB{r, g, b}
}
const (
Reset Attribute = iota
Bold
Faint
Italic
Underline
BlinkSlow
BlinkRapid
Invert
Conceal
CrossedOut
)
const (
DoubleUnderline Attribute = iota + 21 // Can be disable bold on some terminals
DisableBold
DisableItalic
DisableUnderline
DisableBlink
DisableInvert
DisableConceal
DisableCrossedOut
)
const (
FgNone = 0
FgBlack FgColor = iota + 30
FgRed
FgGreen
FgYellow
FgBlue
FgMagenta
FgCyan
FgWhite
FgDefault FgColor = 39
)
const (
BgNone = 0
BgBlack BgColor = iota + 40
BgRed
BgGreen
BgYellow
BgBlue
BgMagenta
BgCyan
BgWhite
BgDefault BgColor = 49
)
const (
FgHiBlack FgColor = iota + 90
FgHiRed
FgHiGreen
FgHiYellow
FgHiBlue
FgHiMagenta
FgHiCyan
FgHiWhite
)
const (
BgHiBlack BgColor = iota + 100
BgHiRed
BgHiGreen
BgHiYellow
BgHiBlue
BgHiMagenta
BgHiCyan
BgHiWhite
)
// This attributes rarely used and not supported by all terminals.
const (
Border Attribute = iota + 51
Outline
Upperline
DisableBorderOutline
DisableUpperline
)
type ColorStringBuilder struct {
str string
}
// NewColorStringBuilder creates a builder for composing ANSI-colored strings.
func NewColorStringBuilder() *ColorStringBuilder {
return &ColorStringBuilder{str: ""}
}
// AddAttribute appends an ANSI text attribute sequence.
func (builder *ColorStringBuilder) AddAttribute(attr Attribute) *ColorStringBuilder {
builder.str += attr.String()
return builder
}
// AddFgColor appends a basic ANSI foreground color sequence.
func (builder *ColorStringBuilder) AddFgColor(color FgColor) *ColorStringBuilder {
if color <= 0 {
return builder
}
builder.str += color.String()
return builder
}
// AddBgColor appends a basic ANSI background color sequence.
func (builder *ColorStringBuilder) AddBgColor(color BgColor) *ColorStringBuilder {
if color <= 0 {
return builder
}
builder.str += color.String()
return builder
}
// AddForeground256Color appends a 256-color foreground sequence.
func (builder *ColorStringBuilder) AddForeground256Color(color FgColor256) *ColorStringBuilder {
builder.str += color.String()
return builder
}
// AddColor256Fg appends a 256-color foreground sequence.
// Deprecated: use AddForeground256Color. This method will be removed in v3.
func (builder *ColorStringBuilder) AddColor256Fg(color uint8) *ColorStringBuilder {
return builder.AddForeground256Color(FgColor256(color))
}
// AddBackground256Color appends a 256-color background sequence.
func (builder *ColorStringBuilder) AddBackground256Color(color BgColor256) *ColorStringBuilder {
builder.str += color.String()
return builder
}
// AddColor256Bg appends a 256-color background sequence.
// Deprecated: use AddBackground256Color. This method will be removed in v3.
func (builder *ColorStringBuilder) AddColor256Bg(color uint8) *ColorStringBuilder {
return builder.AddBackground256Color(BgColor256(color))
}
// AddForegroundRGB appends an RGB foreground sequence.
func (builder *ColorStringBuilder) AddForegroundRGB(color FgColorRGB) *ColorStringBuilder {
builder.str += color.String()
return builder
}
// AddColorRgbFg appends an RGB foreground sequence.
// Deprecated: use AddForegroundRGB. This method will be removed in v3.
func (builder *ColorStringBuilder) AddColorRgbFg(r, g, b uint8) *ColorStringBuilder {
return builder.AddForegroundRGB(FgColorRGB{r, g, b})
}
// AddBackgroundRGB appends an RGB background sequence.
func (builder *ColorStringBuilder) AddBackgroundRGB(color BgColorRGB) *ColorStringBuilder {
builder.str += color.String()
return builder
}
// AddColorRgbBg appends an RGB background sequence.
// Deprecated: use AddBackgroundRGB. This method will be removed in v3.
func (builder *ColorStringBuilder) AddColorRgbBg(r, g, b uint8) *ColorStringBuilder {
return builder.AddBackgroundRGB(BgColorRGB{r, g, b})
}
// AddReset appends the ANSI reset sequence.
func (builder *ColorStringBuilder) AddReset() *ColorStringBuilder {
builder.str += "\x1b[0m"
return builder
}
// AddText appends plain text to the builder.
func (builder *ColorStringBuilder) AddText(text string) *ColorStringBuilder {
builder.str += text
return builder
}
// String returns the built string and appends a reset sequence when needed.
func (builder *ColorStringBuilder) String() string {
if strings.Contains(builder.str, "\x1b[") && !strings.HasSuffix(builder.str, "\x1b[0m") {
builder.AddReset()
}
return builder.str
}
// String returns the ANSI escape sequence for the attribute.
func (a Attribute) String() string {
return fmt.Sprintf("\x1b[%dm", a)
}
// Uint8 returns the raw attribute value.
func (a Attribute) Uint8() uint8 {
return uint8(a)
}
// Int returns the raw attribute value as int.
func (a Attribute) Int() int {
return int(a)
}
// String returns the ANSI escape sequence for the foreground color.
func (c FgColor) String() string {
return fmt.Sprintf("\x1b[%dm", c)
}
// Uint8 returns the raw foreground color value.
func (c FgColor) Uint8() uint8 {
return uint8(c)
}
// Int returns the raw foreground color value as int.
func (c FgColor) Int() int {
return int(c)
}
// String returns the ANSI escape sequence for the background color.
func (c BgColor) String() string {
return fmt.Sprintf("\x1b[%dm", c)
}
// Uint8 returns the raw background color value.
func (c BgColor) Uint8() uint8 {
return uint8(c)
}
// Int returns the raw background color value as int.
func (c BgColor) Int() int {
return int(c)
}
// String returns the ANSI escape sequence for the 256-color foreground value.
func (c FgColor256) String() string { return fmt.Sprintf("\u001B[38;5;%dm", c) }
// Uint8 returns the raw 256-color foreground value.
func (c FgColor256) Uint8() uint8 { return uint8(c) }
// Int returns the raw 256-color foreground value as int.
func (c FgColor256) Int() int { return int(c) }
// String returns the ANSI escape sequence for the 256-color background value.
func (c BgColor256) String() string { return fmt.Sprintf("\u001B[48;5;%dm", c) }
// Uint8 returns the raw 256-color background value.
func (c BgColor256) Uint8() uint8 { return uint8(c) }
// Int returns the raw 256-color background value as int.
func (c BgColor256) Int() int { return int(c) }
// String returns the ANSI escape sequence for the RGB foreground value.
func (c FgColorRGB) String() string { return fmt.Sprintf("\x1b[38;2;%d;%d;%dm", c[0], c[1], c[2]) }
// Uint8 returns the raw RGB foreground components.
func (c FgColorRGB) Uint8() []uint8 { return c }
// String returns the ANSI escape sequence for the RGB background value.
func (c BgColorRGB) String() string { return fmt.Sprintf("\x1b[48;2;%d;%d;%dm", c[0], c[1], c[2]) }
// Uint8 returns the raw RGB background components.
func (c BgColorRGB) Uint8() []uint8 { return c }
+11 -9
View File
@@ -1,4 +1,4 @@
// Package slog provides a small structured logger that can write the same // Package sneklog provides a small structured logger that can write the same
// record to text and JSON outputs. // record to text and JSON outputs.
// //
// A Logger can fan out records to stdout, files, or any external io.Writer. // A Logger can fan out records to stdout, files, or any external io.Writer.
@@ -8,19 +8,21 @@
// CreateLogger uses the following defaults: // CreateLogger uses the following defaults:
// - prefix: "LOG" // - prefix: "LOG"
// - level: FATAL, which enables info, warn, error, and fatal records // - level: FATAL, which enables info, warn, error, and fatal records
// - text timestamps: enabled
// - text traceback: disabled
// - pretty JSON: disabled // - pretty JSON: disabled
// - text formatter: DefaultTextFormatter
// - JSON formatter: DefaultJsonFormatter
// //
// Call Level(DEBUG) to enable all records, including debug messages. // Call Level(DEBUG) to enable all records, including debug messages.
// AddReplacer can be used to mask or normalize message text before records are
// passed to writers. Call SetFormatter on a writer to customize timestamps,
// traceback fields, colors, and message layout.
// //
// Basic usage: // Basic usage:
// //
// logger := slog.CreateLogger(). // logger := sneklog.CreateLogger().
// Prefix("API"). // Prefix("API").
// Level(slog.DEBUG). // Level(sneklog.DEBUG).
// PrintTraceback(true). // AddReplacer("secret-token", "<redacted>")
// JsonPretty(true)
// //
// text := logger.CreateTextStdoutWriter() // text := logger.CreateTextStdoutWriter()
// jsonFile, err := logger.CreateJsonFileWriter("logs/app.json") // jsonFile, err := logger.CreateJsonFileWriter("logs/app.json")
@@ -29,7 +31,7 @@
// } // }
// //
// logger.AddWriters(text, jsonFile) // logger.AddWriters(text, jsonFile)
// logger.Infoln("service started") // logger.Infoln("service started", "secret-token")
// //
// if err := logger.Close(); err != nil { // if err := logger.Close(); err != nil {
// panic(err) // panic(err)
@@ -39,4 +41,4 @@
// the logger and are closed by Logger.Close. Writers created from existing // the logger and are closed by Logger.Close. Writers created from existing
// io.Writer values through CreateTextWriter or CreateJsonWriter remain owned by // io.Writer values through CreateTextWriter or CreateJsonWriter remain owned by
// the caller and are not closed by the logger. // the caller and are not closed by the logger.
package slog package sneklog
+43 -8
View File
@@ -2,27 +2,50 @@ package main
import ( import (
"bytes" "bytes"
"fmt"
"git.scuroneko.dev/scuroneko/slog" "git.scuroneko.dev/scuroneko/sneklog/v2"
) )
func main() { func main() {
logger := slog.CreateLogger(). httpGet := sneklog.NewLogLevelWithColors(0, "get", sneklog.FgGreen, sneklog.BgNone)
Prefix("EXAMPLE"). httpPost := sneklog.NewLogLevelWithColors(0, "post", sneklog.FgBlue, sneklog.BgNone)
Level(slog.DEBUG). httpDelete := sneklog.NewLogLevel(0, "delete")
JsonPretty(true) httpDelete.SetBackgroundRGB(128, 0, 0)
httpDelete.AddAttribute(sneklog.Italic).AddAttribute(sneklog.Bold)
logger := sneklog.NewLogger().
SetName("EXAMPLE").
SetLevel(sneklog.DEBUG).
SetJSONPretty(true).
AddReplacer("SOME_SECRET", "<Secret>")
sneklog.INFO.SetBgColor(sneklog.BgBlue).SetFgColor(sneklog.FgWhite)
textStdout := logger.CreateTextStdoutWriter() textStdout := logger.CreateTextStdoutWriter()
jsonStdout := logger.CreateJsonStdoutWriter() jsonStdout := logger.CreateJsonStdoutWriter()
formatter := sneklog.NewFormatter().
SetFormat("[%t] [%L] [%N]: %m (%s)").
SetTimeStampFormat(sneklog.Kitchen).
SetTraceBackFormat("%s %f:%n %p")
jsonFormatter := sneklog.NewFormatter().
SetFormat("[%L] [%N] %m").
SetColorOutput(false).
SetTimeStampFormat(sneklog.Kitchen)
textStdout.SetFormatter(formatter)
jsonStdout.SetFormatter(jsonFormatter)
textFile, err := logger.CreateTextFileWriter("logs/text.log") textFile, err := logger.CreateTextFileWriter("logs/text.log")
if err != nil { if err != nil {
_ = logger.Close()
panic(err) panic(err)
} }
jsonFile, err := logger.CreateJsonFileWriter("logs/json.log") jsonFile, err := logger.CreateJsonFileWriter("logs/json.log")
if err != nil { if err != nil {
_ = logger.Close()
panic(err) panic(err)
} }
@@ -43,11 +66,23 @@ func main() {
logger.Warnln("cache miss") logger.Warnln("cache miss")
logger.Errorln("request failed") logger.Errorln("request failed")
logger.Debugln("debug details") logger.Debugln("debug details")
logger.Infoln("sensitive info, SOME_SECRET")
logger.Println(httpGet, "this can be logging of http get request/response...")
logger.Println(httpPost, "...and this can be logging of http post request/response...")
logger.Println(httpDelete, "...but this logging of http delete request/response with highly customizable level!")
if err := logger.Close(); err != nil { if err := logger.Close(); err != nil {
panic(err) panic(err)
} }
fmt.Println("external buffer contents:") s := sneklog.NewColorStringBuilder().
fmt.Println(externalBuffer.String()) AddBackgroundRGB(sneklog.NewBgColorRGB(31, 41, 40)).
AddForegroundRGB(sneklog.NewFgColorRGB(220, 215, 186)).
AddAttribute(sneklog.Italic).AddAttribute(sneklog.Bold).
AddText("Some Very very very cool stuff, themed in Kanagawa colors!").String()
println(s)
//fmt.Println("external buffer contents:")
//fmt.Println(externalBuffer.String())
} }
+206
View File
@@ -0,0 +1,206 @@
package sneklog
import (
"fmt"
"strings"
"time"
)
// Formatter
// Format: %t - time, %N - name, %l - level, %L - level uppercase, %b - traceback, %B - full traceback, %m - message,
// %M - method, %f - filename, %n - line number, %s - method signature, %p - full path
// Example: "%t [%l] %N: %m (%f:%n %s)"
//
// TimeStampFormat: %Y - year, %m - month, %d - day, %H - hour, %M - minute, %S - second,
// %z - timezone(i.e. 0300), %Z - timezone(i.e. 03:00)
// Example: "%d.%m.%Y %H:%M:%S"
//
// TraceBackFormat: %M - method, %f - filename, %n - line number, %s - method signature, %p - full path
type Formatter struct {
Format string
MessageSeparator string
// JSONTraceBackPath
// If true, in traceback will be path to file, otherwise only filename.
JSONTraceBackPath bool
TimeStampFormat string
TraceBackFormat string
TraceBackSeparator string
ColorOutput bool
ColorOnlyStdout bool
}
var DefaultTextFormatter = &Formatter{
Format: "%t %l %N: %m",
MessageSeparator: " ",
TimeStampFormat: RFC3339,
TraceBackFormat: "%M (%f:%n %s)",
TraceBackSeparator: "->",
ColorOutput: true,
ColorOnlyStdout: true,
}
var DefaultJsonFormatter = &Formatter{
Format: "%m",
MessageSeparator: " ",
TimeStampFormat: RFC3339,
ColorOutput: false,
}
// NewFormatter returns a copy of the default text formatter settings.
func NewFormatter() *Formatter {
return &Formatter{
Format: DefaultTextFormatter.Format,
MessageSeparator: DefaultTextFormatter.MessageSeparator,
TimeStampFormat: DefaultTextFormatter.TimeStampFormat,
TraceBackFormat: DefaultTextFormatter.TraceBackFormat,
TraceBackSeparator: DefaultTextFormatter.TraceBackSeparator,
ColorOutput: DefaultTextFormatter.ColorOutput,
ColorOnlyStdout: DefaultTextFormatter.ColorOnlyStdout,
}
}
// SetFormat sets the formatter template used for text rendering.
func (f *Formatter) SetFormat(format string) *Formatter {
f.Format = format
return f
}
// SetMessageSeparator sets the separator used to join message parts.
func (f *Formatter) SetMessageSeparator(separator string) *Formatter {
f.MessageSeparator = separator
return f
}
// SetTimeStampFormat sets the strftime-like format used for timestamps.
func (f *Formatter) SetTimeStampFormat(format string) *Formatter {
f.TimeStampFormat = format
return f
}
// SetTraceBackFormat sets the format used for a single traceback frame.
func (f *Formatter) SetTraceBackFormat(format string) *Formatter {
f.TraceBackFormat = format
return f
}
// SetTraceBackSeparator sets the separator used between traceback frames.
func (f *Formatter) SetTraceBackSeparator(separator string) *Formatter {
f.TraceBackSeparator = separator
return f
}
// SetColorOutput enables or disables colorized output.
func (f *Formatter) SetColorOutput(color bool) *Formatter {
f.ColorOutput = color
return f
}
// SetColorOnlyStdout restricts colorized output to stdout and stderr when enabled.
func (f *Formatter) SetColorOnlyStdout(only bool) *Formatter {
f.ColorOnlyStdout = only
return f
}
// FormatMessage applies the formatter template to the provided log record fields.
func (f *Formatter) FormatMessage(level LogLevel, prefix string, tb []*MethodTraceback, messages ...any) string {
if f == nil {
return fmt.Sprint(messages...)
}
output := f.Format
output = strings.ReplaceAll(output, "%t", f.FormatTime(time.Now()))
output = strings.ReplaceAll(output, "%N", prefix)
output = strings.ReplaceAll(output, "%l", level.GetName())
output = strings.ReplaceAll(output, "%L", strings.ToUpper(level.GetName()))
if len(tb) > 0 {
formattedTraceback := f.FormatTraceback(tb[0])
output = strings.ReplaceAll(output, "%b", formattedTraceback)
formattedTraceback = f.FormatTracebacks(tb)
output = strings.ReplaceAll(output, "%B", formattedTraceback)
} else {
output = strings.ReplaceAll(output, "%b", "")
output = strings.ReplaceAll(output, "%B", "")
}
if len(tb) > 0 {
output = strings.ReplaceAll(output, "%M", tb[0].Method)
output = strings.ReplaceAll(output, "%f", tb[0].Filename)
output = strings.ReplaceAll(output, "%n", fmt.Sprintf("%d", tb[0].Line))
output = strings.ReplaceAll(output, "%s", tb[0].Signature)
} else {
output = strings.ReplaceAll(output, "%M", "")
output = strings.ReplaceAll(output, "%f", "")
output = strings.ReplaceAll(output, "%n", "")
output = strings.ReplaceAll(output, "%s", "")
}
if f.JSONTraceBackPath && len(tb) > 0 {
output = strings.ReplaceAll(output, "%p", tb[0].FullPath)
} else {
output = strings.ReplaceAll(output, "%p", "")
}
message := Map(messages, func(m any) string { return fmt.Sprint(m) })
output = strings.ReplaceAll(output, "%m", strings.Join(message, f.MessageSeparator))
return output
}
// ColorizeString wraps a string in the ANSI color sequences defined by the level.
func (f *Formatter) ColorizeString(s string, level LogLevel) string {
builder := NewColorStringBuilder()
if level.fgRgb != nil {
builder.AddForegroundRGB(level.fgRgb)
} else if level.fg256 > 0 {
builder.AddForeground256Color(level.fg256)
} else {
builder.AddFgColor(level.fg)
}
if level.bgRgb != nil {
builder.AddBackgroundRGB(level.bgRgb)
} else if level.bg256 > 0 {
builder.AddBackground256Color(level.bg256)
} else {
builder.AddBgColor(level.bg)
}
for _, attr := range level.attrs {
builder.AddAttribute(attr)
}
return builder.AddText(s).AddReset().String()
}
// FormatTime formats a timestamp using the formatter timestamp layout.
func (f *Formatter) FormatTime(t time.Time) string {
if f.TimeStampFormat == "" {
return t.Format(time.RFC3339)
}
return t.Format(StrftimeToGo(f.TimeStampFormat))
}
// FormatTraceback formats a single traceback frame.
func (f *Formatter) FormatTraceback(traceback *MethodTraceback) string {
formattedTraceback := f.TraceBackFormat
if f.JSONTraceBackPath {
formattedTraceback = strings.ReplaceAll(formattedTraceback, "%p", traceback.FullPath)
} else {
formattedTraceback = strings.ReplaceAll(formattedTraceback, "%p", "")
}
formattedTraceback = strings.ReplaceAll(formattedTraceback, "%M", traceback.Method)
formattedTraceback = strings.ReplaceAll(formattedTraceback, "%f", traceback.Filename)
formattedTraceback = strings.ReplaceAll(formattedTraceback, "%n", fmt.Sprintf("%d", traceback.Line))
formattedTraceback = strings.ReplaceAll(formattedTraceback, "%s", traceback.Signature)
return formattedTraceback
}
// FormatTracebacks formats and joins multiple traceback frames.
func (f *Formatter) FormatTracebacks(traceback []*MethodTraceback) string {
var formattedTraceback []string
for _, frame := range traceback {
formattedTraceback = append(formattedTraceback, f.FormatTraceback(frame))
}
return strings.Join(formattedTraceback, f.TraceBackSeparator)
}
+1 -9
View File
@@ -1,11 +1,3 @@
module git.scuroneko.dev/scuroneko/slog module git.scuroneko.dev/scuroneko/sneklog/v2
go 1.26 go 1.26
require github.com/fatih/color v1.18.0
require (
github.com/mattn/go-colorable v0.1.14 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
golang.org/x/sys v0.42.0 // indirect
)
-9
View File
@@ -1,9 +0,0 @@
github.com/fatih/color v1.18.0 h1:S8gINlzdQ840/4pfAwic/ZE0djQEH3wM94VfqLTZcOM=
github.com/fatih/color v1.18.0/go.mod h1:4FelSpRwEGDpQ12mAdzqdOukCy4u8WUtOY6lkT/6HfU=
github.com/mattn/go-colorable v0.1.14 h1:9A9LHSqF/7dyVVX6g0U9cwm9pG3kP9gSzcuIPHPsaIE=
github.com/mattn/go-colorable v0.1.14/go.mod h1:6LmQG8QLFO4G5z1gPvYEzlUgJ2wF+stgPZH1UqBm1s8=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.42.0 h1:omrd2nAlyT5ESRdCLYdm3+fMfNFE/+Rf4bDIQImRJeo=
golang.org/x/sys v0.42.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
+40 -29
View File
@@ -1,4 +1,4 @@
package slog package sneklog
import ( import (
"fmt" "fmt"
@@ -7,75 +7,85 @@ import (
// Infof logs a formatted info message. // Infof logs a formatted info message.
func (l *Logger) Infof(format string, args ...any) { func (l *Logger) Infof(format string, args ...any) {
l.print(INFO, fmt.Sprintf(format, args...)) l.Print(INFO, fmt.Sprintf(format, args...))
} }
// Info logs an info message. // Info logs an info message.
func (l *Logger) Info(m ...any) { func (l *Logger) Info(m ...any) {
l.print(INFO, m...) l.Print(INFO, m...)
} }
// Infoln logs an info message and appends a newline semantic for writers that need it. // Infoln logs an info message and appends a newline semantic for writers that need it.
func (l *Logger) Infoln(m ...any) { func (l *Logger) Infoln(m ...any) {
l.println(INFO, m...) l.Println(INFO, m...)
} }
// Warnf logs a formatted warning message. // Warnf logs a formatted warning message.
func (l *Logger) Warnf(format string, args ...any) { func (l *Logger) Warnf(format string, args ...any) {
l.print(WARN, fmt.Sprintf(format, args...)) l.Print(WARN, fmt.Sprintf(format, args...))
} }
// Warn logs a warning message. // Warn logs a warning message.
func (l *Logger) Warn(m ...any) { func (l *Logger) Warn(m ...any) {
l.print(WARN, m...) l.Print(WARN, m...)
} }
// Warnln logs a warning message with newline semantic. // Warnln logs a warning message with newline semantic.
func (l *Logger) Warnln(m ...any) { func (l *Logger) Warnln(m ...any) {
l.println(WARN, m...) l.Println(WARN, m...)
} }
// Errorf logs a formatted error message. // Errorf logs a formatted error message.
func (l *Logger) Errorf(format string, args ...any) { func (l *Logger) Errorf(format string, args ...any) {
l.print(ERROR, fmt.Sprintf(format, args...)) l.Print(ERROR, fmt.Sprintf(format, args...))
} }
// Error logs an error message. // Error logs an error message.
func (l *Logger) Error(m ...any) { func (l *Logger) Error(m ...any) {
l.print(ERROR, m...) l.Print(ERROR, m...)
} }
// Errorln logs an error message with newline semantic. // Errorln logs an error message with newline semantic.
func (l *Logger) Errorln(m ...any) { func (l *Logger) Errorln(m ...any) {
l.println(ERROR, m...) l.Println(ERROR, m...)
} }
// Fatalf logs a formatted fatal message and exits the process with code 1. // Fatalf logs a formatted fatal message and exits the process with code 1.
func (l *Logger) Fatalf(format string, args ...any) { func (l *Logger) Fatalf(format string, args ...any) {
l.print(FATAL, fmt.Sprintf(format, args...)) l.Print(FATAL, fmt.Sprintf(format, args...))
os.Exit(1) os.Exit(1)
} }
// Fatal logs a fatal message and exits the process with code 1. // Fatal logs a fatal message and exits the process with code 1.
func (l *Logger) Fatal(m ...any) { func (l *Logger) Fatal(m ...any) {
l.print(FATAL, m...) l.Print(FATAL, m...)
os.Exit(1) os.Exit(1)
} }
// Fatalln logs a fatal message with newline semantic and exits the process with code 1. // Fatalln logs a fatal message with newline semantic and exits the process with code 1.
func (l *Logger) Fatalln(m ...any) { func (l *Logger) Fatalln(m ...any) {
l.println(FATAL, m...) l.Println(FATAL, m...)
os.Exit(1) os.Exit(1)
} }
// Debugf logs a formatted debug message. // Debugf logs a formatted debug message.
func (l *Logger) Debugf(format string, args ...any) { func (l *Logger) Debugf(format string, args ...any) {
l.print(DEBUG, fmt.Sprintf(format, args...)) l.Print(DEBUG, fmt.Sprintf(format, args...))
}
// Debug logs a debug message.
func (l *Logger) Debug(m ...any) {
l.print(DEBUG, m...)
}
// Debugln logs a debug message with newline semantic.
func (l *Logger) Debugln(m ...any) {
l.println(DEBUG, m...)
} }
// Write message without trailing "\n" // Debug logs a debug message.
func (l *Logger) Debug(m ...any) {
l.Print(DEBUG, m...)
}
// Debugln logs a debug message with newline semantic.
func (l *Logger) Debugln(m ...any) {
l.Println(DEBUG, m...)
}
// Print write message without trailing "\n"
// Good for database // Good for database
func (l *Logger) print(level LogLevel, m ...any) { func (l *Logger) Print(level LogLevel, m ...any) {
if l.level.n < level.n { if l.level.n < level.n {
return return
} }
@@ -83,21 +93,22 @@ func (l *Logger) print(level LogLevel, m ...any) {
return return
} }
tb := getFullTraceback(0) tb := getFullTraceback(1)
for _, writer := range l.writers { for _, writer := range l.writers {
if writer == nil { if writer == nil {
continue continue
} }
err := writer.Print(level, l.prefix, tb, m...) err := writer.Print(level, l.prefix, tb, l.replaceAll(m...)...)
if err != nil { if err != nil {
l.reportWriterError(err) l.reportWriterError(err)
} }
} }
} }
// Println
// Docker requires "\n" at end to write to log. // Docker requires "\n" at end to write to log.
// print not work for docker, otherwise it will work and write into stdout // print not work for docker, otherwise it will work and write into stdout
func (l *Logger) println(level LogLevel, m ...any) { func (l *Logger) Println(level LogLevel, m ...any) {
if l.level.n < level.n { if l.level.n < level.n {
return return
} }
@@ -105,8 +116,8 @@ func (l *Logger) println(level LogLevel, m ...any) {
return return
} }
tb := getFullTraceback(0) tb := getFullTraceback(1)
messages := append(append(make([]any, 0, len(m)+1), m...), "\n") messages := append(append(make([]any, 0, len(m)+1), l.replaceAll(m...)...), "\n")
for _, writer := range l.writers { for _, writer := range l.writers {
if writer == nil { if writer == nil {
continue continue
+230 -73
View File
@@ -1,36 +1,158 @@
package slog package sneklog
import ( import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"strings" "strings"
"time"
"github.com/fatih/color"
) )
// Logger routes log records to one or more configured writers.
type Logger struct {
prefix string
level LogLevel
writers []LoggerWriter
printTraceback bool
printTime bool
jsonPretty bool
}
// LogLevel describes a logging severity. // LogLevel describes a logging severity.
type LogLevel struct { type LogLevel struct {
n uint8 n uint8
t string t string
c color.Attribute
fg FgColor
fg256 FgColor256
fgRgb FgColorRGB
bg BgColor
bg256 BgColor256
bgRgb BgColorRGB
attrs []Attribute
}
// NewLogLevel creates a log level without predefined colors.
func NewLogLevel(index uint8, name string) LogLevel {
return LogLevel{n: index, t: name, attrs: []Attribute{}}
}
// NewLogLevelWithColors creates a log level with ANSI foreground and background colors.
func NewLogLevelWithColors(index uint8, name string, fg FgColor, bg BgColor) LogLevel {
return LogLevel{n: index, t: name, fg: fg, bg: bg, attrs: []Attribute{}}
} }
// GetName returns the lowercase textual representation of the level. // GetName returns the lowercase textual representation of the level.
func (l *LogLevel) GetName() string { func (l *LogLevel) GetName() string { return l.t }
return l.t
// SetFgColor sets the ANSI foreground color and clears other foreground color modes.
// Deprecated: use SetForegroundColor. This method will be removed in v3.
func (l *LogLevel) SetFgColor(color FgColor) *LogLevel {
return l.SetForegroundColor(color)
}
// GetFgColor returns the ANSI foreground color for the level.
// Deprecated: use GetForegroundColor. This method will be removed in v3.
func (l *LogLevel) GetFgColor() FgColor { return l.GetForegroundColor() }
// SetForegroundColor sets the ANSI foreground color and clears other foreground color modes.
func (l *LogLevel) SetForegroundColor(color FgColor) *LogLevel {
l.fg = color
l.fg256 = 0
l.fgRgb = nil
return l
}
// GetForegroundColor returns the ANSI foreground color for the level.
func (l *LogLevel) GetForegroundColor() FgColor { return l.fg }
// SetForeground256Color sets the 256-color foreground and clears other foreground color modes.
func (l *LogLevel) SetForeground256Color(color FgColor256) *LogLevel {
l.fg = 0
l.fg256 = color
l.fgRgb = nil
return l
}
// GetForeground256Color returns the configured 256-color foreground value.
func (l *LogLevel) GetForeground256Color() FgColor256 { return l.fg256 }
// SetForegroundRGB sets the RGB foreground color and clears other foreground color modes.
func (l *LogLevel) SetForegroundRGB(r, g, b uint8) *LogLevel {
l.fg = 0
l.fg256 = 0
l.fgRgb = FgColorRGB{r, g, b}
return l
}
// GetForegroundRGB returns the configured RGB foreground value.
func (l *LogLevel) GetForegroundRGB() FgColorRGB { return l.fgRgb }
// SetBgColor sets the ANSI background color and clears other background color modes.
// Deprecated: use SetBackgroundColor. This method will be removed in v3.
func (l *LogLevel) SetBgColor(color BgColor) *LogLevel {
return l.SetBackgroundColor(color)
}
// GetBgColor returns the ANSI background color for the level.
// Deprecated: use GetBackgroundColor. This method will be removed in v3.
func (l *LogLevel) GetBgColor() BgColor { return l.GetBackgroundColor() }
// SetBackgroundColor sets the ANSI background color and clears other background color modes.
func (l *LogLevel) SetBackgroundColor(color BgColor) *LogLevel {
l.bg = color
l.bg256 = 0
l.bgRgb = nil
return l
}
// GetBackgroundColor returns the ANSI background color for the level.
func (l *LogLevel) GetBackgroundColor() BgColor { return l.bg }
// SetBackground256Color sets the 256-color background and clears other background color modes.
func (l *LogLevel) SetBackground256Color(color BgColor256) *LogLevel {
l.bg = 0
l.bg256 = color
l.bgRgb = nil
return l
}
// GetBackground256Color returns the configured 256-color background value.
func (l *LogLevel) GetBackground256Color() BgColor256 { return l.bg256 }
// SetBackgroundRGB sets the RGB background color and clears other background color modes.
func (l *LogLevel) SetBackgroundRGB(r, g, b uint8) *LogLevel {
l.bg = 0
l.bg256 = 0
l.bgRgb = BgColorRGB{r, g, b}
return l
}
// GetBackgroundRGB returns the configured RGB background value.
func (l *LogLevel) GetBackgroundRGB() BgColorRGB { return l.bgRgb }
// AddAttribute appends an ANSI text attribute to the level.
func (l *LogLevel) AddAttribute(a Attribute) *LogLevel {
l.attrs = append(l.attrs, a)
return l
}
// RemoveAttribute removes all matching ANSI text attributes from the level.
func (l *LogLevel) RemoveAttribute(a Attribute) *LogLevel {
attrs := make([]Attribute, 0)
for _, attr := range l.attrs {
if attr != a {
attrs = append(attrs, attr)
}
}
l.attrs = attrs
return l
}
// SetAttributes replaces the level attributes with a copy of the provided slice.
func (l *LogLevel) SetAttributes(a []Attribute) *LogLevel {
attrs := make([]Attribute, len(a))
copy(attrs, a)
l.attrs = attrs
return l
}
// GetAttributes returns a copy of the configured ANSI text attributes.
func (l *LogLevel) GetAttributes() []Attribute {
attrs := make([]Attribute, len(l.attrs))
copy(attrs, l.attrs)
return attrs
} }
// MethodTraceback describes a single stack frame attached to a log entry. // MethodTraceback describes a single stack frame attached to a log entry.
@@ -44,58 +166,106 @@ type MethodTraceback struct {
// Predefined log levels. // Predefined log levels.
var ( var (
INFO = LogLevel{n: 0, t: "info", c: color.FgWhite} INFO = NewLogLevelWithColors(0, "info", FgWhite, BgNone)
WARN = LogLevel{n: 1, t: "warn", c: color.FgHiYellow} WARN = NewLogLevelWithColors(1, "warn", FgHiYellow, BgNone)
ERROR = LogLevel{n: 2, t: "error", c: color.FgHiRed} ERROR = NewLogLevelWithColors(2, "error", FgHiRed, BgNone)
FATAL = LogLevel{n: 3, t: "fatal", c: color.FgRed} FATAL = NewLogLevelWithColors(3, "fatal", FgRed, BgNone)
DEBUG = LogLevel{n: 4, t: "debug", c: color.FgGreen} DEBUG = NewLogLevelWithColors(4, "debug", FgGreen, BgNone)
) )
// Logger routes log records to one or more configured writers.
type Logger struct {
prefix string
level LogLevel
writers []LoggerWriter
replacers []replacer
jsonPretty bool
}
// CreateLogger creates a logger with default settings. // CreateLogger creates a logger with default settings.
// Deprecated: use NewLogger. This method will be removed in v3.
func CreateLogger() *Logger { func CreateLogger() *Logger {
return &Logger{ return &Logger{
prefix: "LOG", prefix: "LOG",
level: FATAL, level: FATAL,
printTraceback: false, }
printTime: true, }
// NewLogger creates a logger with default settings.
func NewLogger() *Logger {
return &Logger{
prefix: "LOG",
level: FATAL,
} }
} }
// Prefix sets the record prefix and returns the logger for chaining. // Prefix sets the record prefix and returns the logger for chaining.
// Deprecated: use SetName. This method will be removed in v3.
func (l *Logger) Prefix(prefix string) *Logger { func (l *Logger) Prefix(prefix string) *Logger {
l.prefix = prefix l.prefix = prefix
return l return l
} }
// SetName sets the record prefix and returns the logger for chaining.
func (l *Logger) SetName(name string) *Logger {
l.prefix = name
return l
}
// Level sets the maximum enabled level and returns the logger for chaining. // Level sets the maximum enabled level and returns the logger for chaining.
// Deprecated: use SetLevel. This method will be removed in v3.
func (l *Logger) Level(level LogLevel) *Logger { func (l *Logger) Level(level LogLevel) *Logger {
l.level = level l.level = level
return l return l
} }
// PrintTraceback enables traceback output for text writers.
func (l *Logger) PrintTraceback(b bool) *Logger { // SetLevel sets the maximum enabled level and returns the logger for chaining.
l.printTraceback = b func (l *Logger) SetLevel(level LogLevel) *Logger {
l.level = level
return l return l
} }
// PrintTime enables timestamps for text writers.
func (l *Logger) PrintTime(b bool) *Logger { // JsonPretty enables or disables indented JSON output for JSON writers created by the logger.
l.printTime = b // Deprecated: use SetJSONPretty. This method will be removed in v3.
return l
}
// JsonPretty enables indented JSON output for JSON writers.
func (l *Logger) JsonPretty(b bool) *Logger { func (l *Logger) JsonPretty(b bool) *Logger {
l.jsonPretty = b l.jsonPretty = b
return l return l
} }
// SetJSONPretty enables or disables indented output for JSON writers created by the logger.
func (l *Logger) SetJSONPretty(b bool) *Logger {
l.jsonPretty = b
return l
}
// AddWriters appends multiple writers to the logger. // AddWriters appends multiple writers to the logger.
func (l *Logger) AddWriters(writers ...LoggerWriter) *Logger { func (l *Logger) AddWriters(writers ...LoggerWriter) *Logger {
l.writers = append(l.writers, writers...) l.writers = append(l.writers, writers...)
return l return l
} }
// AddWriter appends a single writer to the logger. // AddWriter appends a single writer to the logger.
// Deprecated: use AddWriters. This method will be removed in v3.
func (l *Logger) AddWriter(writer LoggerWriter) *Logger { func (l *Logger) AddWriter(writer LoggerWriter) *Logger {
l.writers = append(l.writers, writer) l.writers = append(l.writers, writer)
return l return l
} }
// AddReplacer appends a message replacer and returns the logger for chaining.
//
// Replacement rules are applied before records are passed to writers. Empty old
// values are ignored because replacing an empty string would insert the
// replacement between every UTF-8 sequence.
func (l *Logger) AddReplacer(old, new string) *Logger {
if old == "" {
return l
}
r := replacer{old: old, new: new}
l.replacers = append(l.replacers, r)
return l
}
// Close closes all owned writers and returns a joined error, if any. // Close closes all owned writers and returns a joined error, if any.
func (l *Logger) Close() error { func (l *Logger) Close() error {
var errs []error var errs []error
@@ -113,64 +283,51 @@ func (l *Logger) Close() error {
} }
// CreateTextWriter wraps an external writer with the logger text settings. // CreateTextWriter wraps an external writer with the logger text settings.
func (l *Logger) CreateTextWriter(w io.Writer) *LoggerTextWriter { func (l *Logger) CreateTextWriter(w io.Writer) *LoggerTextWriter { return CreateTextWriter(w) }
return CreateTextWriter(w, l.printTraceback, l.printTime)
}
// CreateTextStdoutWriter creates a non-owning text writer for os.Stdout. // CreateTextStdoutWriter creates a non-owning text writer for os.Stdout.
func (l *Logger) CreateTextStdoutWriter() *LoggerTextWriter { func (l *Logger) CreateTextStdoutWriter() *LoggerTextWriter { return CreateTextStdoutWriter() }
return CreateTextStdoutWriter(l.printTraceback, l.printTime)
}
// CreateTextFileWriter creates an owning text writer for a file. // CreateTextFileWriter creates an owning text writer for a file.
func (l *Logger) CreateTextFileWriter(filename string) (*LoggerTextWriter, error) { func (l *Logger) CreateTextFileWriter(filename string) (*LoggerTextWriter, error) {
return CreateTextFileWriter(filename, l.printTraceback, l.printTime) return CreateTextFileWriter(filename)
} }
// CreateJsonWriter wraps an external writer with the logger JSON settings. // CreateJsonWriter wraps an external writer with the logger JSON settings.
func (l *Logger) CreateJsonWriter(w io.Writer) *LoggerJsonWriter { func (l *Logger) CreateJsonWriter(w io.Writer) *LoggerJsonWriter {
return CreateJsonWriter(w, l.jsonPretty) return CreateJsonWriter(w, l.jsonPretty)
} }
// CreateJsonStdoutWriter creates a non-owning JSON writer for os.Stdout. // CreateJsonStdoutWriter creates a non-owning JSON writer for os.Stdout.
func (l *Logger) CreateJsonStdoutWriter() *LoggerJsonWriter { func (l *Logger) CreateJsonStdoutWriter() *LoggerJsonWriter {
return CreateJsonStdoutWriter(l.jsonPretty) return CreateJsonStdoutWriter(l.jsonPretty)
} }
// CreateJsonFileWriter creates an owning JSON writer for a file. // CreateJsonFileWriter creates an owning JSON writer for a file.
func (l *Logger) CreateJsonFileWriter(filename string) (*LoggerJsonWriter, error) { func (l *Logger) CreateJsonFileWriter(filename string) (*LoggerJsonWriter, error) {
return CreateJsonFileWriter(filename, l.jsonPretty) return CreateJsonFileWriter(filename, l.jsonPretty)
} }
// FormatTime converts time to the package text log timestamp format. type replacer struct {
func FormatTime(t time.Time) string { old string
return fmt.Sprintf("%02d.%02d.%02d %02d:%02d:%02d", t.Day(), t.Month(), t.Year(), t.Hour(), t.Minute(), t.Second()) new string
}
// FormatTraceback converts a traceback frame to a compact string.
func FormatTraceback(mt *MethodTraceback) string {
return fmt.Sprintf("%s:%s:%d", mt.Filename, mt.Method, mt.Line)
}
// FormatFullTraceback joins multiple traceback frames into one string.
func FormatFullTraceback(tracebacks []*MethodTraceback) string {
formatted := make([]string, 0)
for _, tb := range tracebacks {
formatted = append(formatted, FormatTraceback(tb))
}
return strings.Join(formatted, "->")
}
// BuildString renders a text log record using the provided settings.
func BuildString(level LogLevel, prefix string, printTime, printTraceback bool, m ...any) string {
args := []string{
fmt.Sprintf("[%s]", prefix),
fmt.Sprintf("[%s]", strings.ToUpper(level.t)),
} }
if printTraceback { func (r replacer) replace(s string) string { return strings.ReplaceAll(s, r.old, r.new) }
args = append(args, fmt.Sprintf("[%s]", FormatTraceback(getTraceback()))) func (l *Logger) replace(s string) string {
out := s
for _, repl := range l.replacers {
out = repl.replace(out)
} }
return out
if printTime {
args = append(args, fmt.Sprintf("[%s]", FormatTime(time.Now())))
} }
func (l *Logger) replaceAll(messages ...any) []any {
msg := Map(m, func(el any) string { if len(l.replacers) == 0 {
return fmt.Sprintf("%v", el) return messages
}) }
s := fmt.Sprintf("%s %s", strings.Join(args, " "), strings.Join(msg, " ")) out := make([]any, len(messages))
return s for i, msg := range messages {
out[i] = l.replace(fmt.Sprint(msg))
}
return out
} }
+313 -8
View File
@@ -1,7 +1,8 @@
package slog package sneklog
import ( import (
"bytes" "bytes"
"encoding/json"
"errors" "errors"
"os" "os"
"strings" "strings"
@@ -13,8 +14,12 @@ type stubLoggerWriter struct {
closeErr error closeErr error
printCalls int printCalls int
closeCalls int closeCalls int
messages []any
} }
func (w *stubLoggerWriter) Formatter() *Formatter {
return &Formatter{}
}
func (w *stubLoggerWriter) Close() error { func (w *stubLoggerWriter) Close() error {
w.closeCalls++ w.closeCalls++
return w.closeErr return w.closeErr
@@ -24,8 +29,9 @@ func (w *stubLoggerWriter) Write(p []byte) (int, error) {
return len(p), nil return len(p), nil
} }
func (w *stubLoggerWriter) Print(_ LogLevel, _ string, _ []*MethodTraceback, _ ...any) error { func (w *stubLoggerWriter) Print(_ LogLevel, _ string, _ []*MethodTraceback, messages ...any) error {
w.printCalls++ w.printCalls++
w.messages = append([]any(nil), messages...)
return w.printErr return w.printErr
} }
@@ -80,8 +86,22 @@ func TestLoggerPrintDoesNotRecurseOnWriterError(t *testing.T) {
} }
} }
func TestLoggerPreservesMessageTypesWithoutReplacers(t *testing.T) {
writer := &stubLoggerWriter{}
logger := CreateLogger().AddWriter(writer)
logger.Error("status", 500)
if len(writer.messages) != 2 {
t.Fatalf("writer should receive two messages, got %d", len(writer.messages))
}
if _, ok := writer.messages[1].(int); !ok {
t.Fatalf("writer should receive original int message type, got %T", writer.messages[1])
}
}
func TestCreateTextWriterCloseOnNonCloserIsNoOp(t *testing.T) { func TestCreateTextWriterCloseOnNonCloserIsNoOp(t *testing.T) {
writer := CreateTextWriter(&bytes.Buffer{}, false, false) writer := CreateTextWriter(&bytes.Buffer{})
if err := writer.Close(); err != nil { if err := writer.Close(); err != nil {
t.Fatalf("Close() error = %v", err) t.Fatalf("Close() error = %v", err)
} }
@@ -96,7 +116,7 @@ func TestCreateTextWriterDoesNotCloseExternalCloser(t *testing.T) {
_ = file.Close() _ = file.Close()
}) })
writer := CreateTextWriter(file, false, false) writer := CreateTextWriter(file)
if err := writer.Close(); err != nil { if err := writer.Close(); err != nil {
t.Fatalf("Close() error = %v", err) t.Fatalf("Close() error = %v", err)
} }
@@ -130,10 +150,295 @@ func TestCreateJsonWriterDoesNotCloseExternalCloser(t *testing.T) {
} }
} }
func TestJsonWriterPrintAllowsEmptyMessages(t *testing.T) {
var buf bytes.Buffer
writer := CreateJsonWriter(&buf, false)
if err := writer.Print(INFO, "TEST", nil); err != nil {
t.Fatalf("Print() error = %v", err)
}
var message LoggerJsonMessage
if err := json.Unmarshal(buf.Bytes(), &message); err != nil {
t.Fatalf("json.Unmarshal() error = %v", err)
}
if message.Message != "" {
t.Fatalf("message should be empty, got %q", message.Message)
}
}
func TestJsonWriterPrintPreservesTrailingNewlineSemantic(t *testing.T) {
var buf bytes.Buffer
writer := CreateJsonWriter(&buf, false)
if err := writer.Print(INFO, "TEST", nil, "hello\n"); err != nil {
t.Fatalf("Print() error = %v", err)
}
data := buf.Bytes()
if !bytes.HasSuffix(data, []byte("\n")) {
t.Fatalf("JSON output should end with newline, got %q", data)
}
var message LoggerJsonMessage
if err := json.Unmarshal(bytes.TrimSuffix(data, []byte("\n")), &message); err != nil {
t.Fatalf("json.Unmarshal() error = %v", err)
}
if message.Message != "hello" {
t.Fatalf("message should not include trailing newline, got %q", message.Message)
}
}
func TestTextWriterPrintlnDoesNotLeaveTrailingMessageSeparator(t *testing.T) {
var buf bytes.Buffer
formatter := NewFormatter().
SetFormat("%m (%S)").
SetColorOutput(false)
writer := CreateTextWriter(&buf).SetFormatter(formatter)
if err := writer.Print(DEBUG, "TEST", nil, "debug details", "\n"); err != nil {
t.Fatalf("Print() error = %v", err)
}
got := strings.TrimSuffix(buf.String(), "\n")
if got != "debug details (%S)" {
t.Fatalf("println newline marker should not leave trailing message separator, got %q", got)
}
}
func TestTextWriterPrintlnDoesNotLeaveTrailingMessageSeparatorAfterReplacement(t *testing.T) {
var buf bytes.Buffer
formatter := NewFormatter().
SetFormat("%m (%S)").
SetColorOutput(false)
writer := CreateTextWriter(&buf).SetFormatter(formatter)
logger := CreateLogger().
SetLevel(DEBUG).
AddWriter(writer).
AddReplacer("details", "details")
logger.Debugln("debug details")
got := strings.TrimSuffix(buf.String(), "\n")
if got != "debug details (%S)" {
t.Fatalf("println newline marker should not leave trailing message separator after replacements, got %q", got)
}
}
func TestJsonWriterPrintlnDoesNotLeaveTrailingMessageSeparator(t *testing.T) {
var buf bytes.Buffer
writer := CreateJsonWriter(&buf, false)
if err := writer.Print(INFO, "TEST", nil, "hello", "\n"); err != nil {
t.Fatalf("Print() error = %v", err)
}
var message LoggerJsonMessage
if err := json.Unmarshal(bytes.TrimSuffix(buf.Bytes(), []byte("\n")), &message); err != nil {
t.Fatalf("json.Unmarshal() error = %v", err)
}
if message.Message != "hello" {
t.Fatalf("message should not include trailing separator from newline marker, got %q", message.Message)
}
}
func TestLogLevelForegroundSettersOverridePreviousColorMode(t *testing.T) {
level := NewLogLevel(0, "custom")
formatter := NewFormatter()
level.SetFgColor(FgRed)
if got := formatter.ColorizeString("x", level); !strings.HasPrefix(got, FgRed.String()) {
t.Fatalf("expected plain foreground color prefix %q, got %q", FgRed.String(), got)
}
level.SetForeground256Color(FgColor256(123))
if got := formatter.ColorizeString("x", level); !strings.HasPrefix(got, FgColor256(123).String()) {
t.Fatalf("expected 256-color foreground prefix %q, got %q", FgColor256(123).String(), got)
}
level.SetForegroundRGB(1, 2, 3)
expectedRGB := NewFgColorRGB(1, 2, 3).String()
if got := formatter.ColorizeString("x", level); !strings.HasPrefix(got, expectedRGB) {
t.Fatalf("expected RGB foreground prefix %q, got %q", expectedRGB, got)
}
level.SetFgColor(FgBlue)
if got := formatter.ColorizeString("x", level); !strings.HasPrefix(got, FgBlue.String()) {
t.Fatalf("expected last plain foreground color to win with prefix %q, got %q", FgBlue.String(), got)
}
}
func TestLogLevelBackgroundSettersOverridePreviousColorMode(t *testing.T) {
level := NewLogLevel(0, "custom")
formatter := NewFormatter()
level.SetBgColor(BgRed)
if got := formatter.ColorizeString("x", level); !strings.HasPrefix(got, BgRed.String()) {
t.Fatalf("expected plain background color prefix %q, got %q", BgRed.String(), got)
}
level.SetBackground256Color(BgColor256(123))
if got := formatter.ColorizeString("x", level); !strings.HasPrefix(got, BgColor256(123).String()) {
t.Fatalf("expected 256-color background prefix %q, got %q", BgColor256(123).String(), got)
}
level.SetBackgroundRGB(1, 2, 3)
expectedRGB := NewBgColorRGB(1, 2, 3).String()
if got := formatter.ColorizeString("x", level); !strings.HasPrefix(got, expectedRGB) {
t.Fatalf("expected RGB background prefix %q, got %q", expectedRGB, got)
}
level.SetBgColor(BgBlue)
if got := formatter.ColorizeString("x", level); !strings.HasPrefix(got, BgBlue.String()) {
t.Fatalf("expected last plain background color to win with prefix %q, got %q", BgBlue.String(), got)
}
}
func TestLogLevelAttributesAffectColorizedOutput(t *testing.T) {
level := NewLogLevel(0, "custom")
level.AddAttribute(Italic).AddAttribute(Bold)
got := NewFormatter().ColorizeString("x", level)
if !strings.Contains(got, Italic.String()) {
t.Fatalf("expected italic attribute in colorized output, got %q", got)
}
if !strings.Contains(got, Bold.String()) {
t.Fatalf("expected bold attribute in colorized output, got %q", got)
}
}
func TestLogLevelRemoveAttributeRemovesAllMatches(t *testing.T) {
level := NewLogLevel(0, "custom")
level.AddAttribute(Bold).AddAttribute(Italic).AddAttribute(Bold)
level.RemoveAttribute(Bold)
got := level.GetAttributes()
if len(got) != 1 || got[0] != Italic {
t.Fatalf("expected only italic attribute to remain, got %#v", got)
}
}
func TestLogLevelSetAttributesCopiesInputSlice(t *testing.T) {
level := NewLogLevel(0, "custom")
attrs := []Attribute{Bold, Italic}
level.SetAttributes(attrs)
attrs[0] = Underline
got := level.GetAttributes()
if len(got) != 2 || got[0] != Bold || got[1] != Italic {
t.Fatalf("expected SetAttributes to copy input slice, got %#v", got)
}
}
func TestLogLevelGetAttributesReturnsCopy(t *testing.T) {
level := NewLogLevel(0, "custom")
level.SetAttributes([]Attribute{Bold, Italic})
got := level.GetAttributes()
got[0] = Underline
again := level.GetAttributes()
if len(again) != 2 || again[0] != Bold || again[1] != Italic {
t.Fatalf("expected GetAttributes to return a copy, got %#v", again)
}
}
func TestLogLevelDeprecatedForegroundAccessorsRemainCompatible(t *testing.T) {
level := NewLogLevel(0, "custom")
level.SetFgColor(FgBlue)
if got := level.GetFgColor(); got != FgBlue {
t.Fatalf("expected deprecated foreground accessors to round-trip %v, got %v", FgBlue, got)
}
level.SetForegroundColor(FgRed)
if got := level.GetFgColor(); got != FgRed {
t.Fatalf("expected deprecated getter to reflect new foreground setter, got %v", got)
}
}
func TestLogLevelDeprecatedBackgroundAccessorsRemainCompatible(t *testing.T) {
level := NewLogLevel(0, "custom")
level.SetBgColor(BgBlue)
if got := level.GetBgColor(); got != BgBlue {
t.Fatalf("expected deprecated background accessors to round-trip %v, got %v", BgBlue, got)
}
level.SetBackgroundColor(BgRed)
if got := level.GetBgColor(); got != BgRed {
t.Fatalf("expected deprecated getter to reflect new background setter, got %v", got)
}
}
func TestFormatterHandlesEmptyTracebackPlaceholders(t *testing.T) {
formatter := NewFormatter().
SetFormat("%m|%b|%B|%M|%f|%n|%s|%p")
got := formatter.FormatMessage(INFO, "TEST", nil, "hello")
if got != "hello|||||||" {
t.Fatalf("empty traceback placeholders should be empty, got %q", got)
}
}
func TestFormatterDoesNotInterpretPlaceholdersInsideMessages(t *testing.T) {
formatter := NewFormatter().
SetFormat("%L:%m")
message := "literal %L %s %p"
got := formatter.FormatMessage(INFO, "TEST", nil, message)
if got != "INFO:literal %L %s %p" {
t.Fatalf("message placeholder-looking text should stay literal, got %q", got)
}
}
func TestNewFormatterDoesNotMutateDefaultFormatter(t *testing.T) {
formatter := NewFormatter()
formatter.SetFormat("custom")
if DefaultTextFormatter.Format == "custom" {
t.Fatal("NewFormatter() should return a copy, not mutate DefaultTextFormatter")
}
}
func TestTextWriterColorOutputHonorsColorOnlyStdoutFalse(t *testing.T) {
var buf bytes.Buffer
formatter := NewFormatter().
SetFormat("%m").
SetColorOutput(true).
SetColorOnlyStdout(false)
writer := CreateTextWriter(&buf).SetFormatter(formatter)
if err := writer.Print(INFO, "TEST", nil, "hello"); err != nil {
t.Fatalf("Print() error = %v", err)
}
if !strings.Contains(buf.String(), "\x1b[") {
t.Fatalf("ColorOnlyStdout(false) should allow color for external writers, got %q", buf.String())
}
}
func TestTextWriterColorOutputCanBeDisabled(t *testing.T) {
var buf bytes.Buffer
formatter := NewFormatter().
SetFormat("%m").
SetColorOutput(false).
SetColorOnlyStdout(false)
writer := CreateTextWriter(&buf).SetFormatter(formatter)
if err := writer.Print(INFO, "TEST", nil, "hello"); err != nil {
t.Fatalf("Print() error = %v", err)
}
if strings.Contains(buf.String(), "\x1b[") {
t.Fatalf("SetColorOutput(false) should disable color output, got %q", buf.String())
}
}
func TestCreateTextStdoutWriterDoesNotCloseStdout(t *testing.T) { func TestCreateTextStdoutWriterDoesNotCloseStdout(t *testing.T) {
stdoutFile := swapStdout(t) stdoutFile := swapStdout(t)
writer := CreateTextStdoutWriter(false, false) writer := CreateTextStdoutWriter()
if err := writer.Close(); err != nil { if err := writer.Close(); err != nil {
t.Fatalf("Close() error = %v", err) t.Fatalf("Close() error = %v", err)
} }
@@ -154,7 +459,7 @@ func TestCreateJsonStdoutWriterDoesNotCloseStdout(t *testing.T) {
} }
} }
func TestGetFullTracebackSkipsRuntimeAndSlogFrames(t *testing.T) { func TestGetFullTracebackSkipsRuntimeAndSneklogFrames(t *testing.T) {
tracebacks := captureTracebackForTest() tracebacks := captureTracebackForTest()
if len(tracebacks) == 0 { if len(tracebacks) == 0 {
t.Fatal("expected at least one traceback frame") t.Fatal("expected at least one traceback frame")
@@ -169,7 +474,7 @@ func TestGetFullTracebackSkipsRuntimeAndSlogFrames(t *testing.T) {
t.Fatalf("runtime frame should be filtered out, got %s", tb.Signature) t.Fatalf("runtime frame should be filtered out, got %s", tb.Signature)
} }
if _, ok := internalTracebackMethods[tb.Method]; ok { if _, ok := internalTracebackMethods[tb.Method]; ok {
t.Fatalf("internal slog frame should be filtered out, got %s", tb.Signature) t.Fatalf("internal sneklog frame should be filtered out, got %s", tb.Signature)
} }
} }
} }
@@ -186,7 +491,7 @@ func TestGetTracebackReturnsNearestUserFrame(t *testing.T) {
t.Fatalf("runtime frame should be filtered out, got %s", tb.Signature) t.Fatalf("runtime frame should be filtered out, got %s", tb.Signature)
} }
if _, ok := internalTracebackMethods[tb.Method]; ok { if _, ok := internalTracebackMethods[tb.Method]; ok {
t.Fatalf("internal slog frame should be filtered out, got %s", tb.Signature) t.Fatalf("internal sneklog frame should be filtered out, got %s", tb.Signature)
} }
} }
+163
View File
@@ -0,0 +1,163 @@
package sneklog
import "strings"
const (
Layout = "%m/%d %I:%M:%S%p '%y %z"
ANSIC = "%a %b %e %H:%M:%S %Y"
UnixDate = "%a %b %e %H:%M:%S %Z %Y"
RubyDate = "%a %b %d %H:%M:%S %z %Y"
RFC822 = "%d %b %y %H:%M %Z"
RFC822Z = "%d %b %y %H:%M %z"
RFC850 = "%A, %d-%b-%y %H:%M:%S %Z"
RFC1123 = "%a, %d %b %Y %H:%M:%S %Z"
RFC1123Z = "%a, %d %b %Y %H:%M:%S %z"
RFC3339 = "%Y-%m-%dT%H:%M:%S%:z"
RFC3339Nano = "%Y-%m-%dT%H:%M:%S%#N%:z"
Kitchen = "%-I:%M%p"
Stamp = "%b %e %H:%M:%S"
StampMilli = "%b %e %H:%M:%S.%L"
StampMicro = "%b %e %H:%M:%S.%f"
StampNano = "%b %e %H:%M:%S.%N"
DateTime = "%Y-%m-%d %H:%M:%S"
DateOnly = "%Y-%m-%d"
TimeOnly = "%H:%M:%S"
)
type rule struct {
strf string
goLayout string
}
var strftimeToGoRules = []rule{
// Long/special tokens first.
{"%::z", "Z07:00:00"},
{"%:::z", "Z07"},
{"%:z", "Z07:00"},
{"%#N", ".999999999"},
{"%#f", ".999999"},
{"%#L", ".999"},
{"%-m", "1"},
{"%#m", "1"},
{"%-d", "2"},
{"%#d", "2"},
{"%-H", "15"},
{"%#H", "15"},
{"%-I", "3"},
{"%#I", "3"},
{"%-M", "4"},
{"%#M", "4"},
{"%-S", "5"},
{"%#S", "5"},
{"%%", "%"},
{"%Y", "2006"},
{"%y", "06"},
{"%B", "January"},
{"%b", "Jan"},
{"%h", "Jan"},
{"%m", "01"},
{"%d", "02"},
{"%e", "_2"},
{"%A", "Monday"},
{"%a", "Mon"},
{"%H", "15"},
{"%I", "03"},
{"%M", "04"},
{"%S", "05"},
{"%p", "PM"},
{"%z", "-0700"},
{"%Z", "MST"},
{"%N", "000000000"},
{"%f", "000000"},
{"%L", "000"},
{"%F", "2006-01-02"},
{"%T", "15:04:05"},
{"%R", "15:04"},
{"%D", "01/02/06"},
{"%r", "03:04:05 PM"},
}
// StrftimeToGo converts a strftime-like layout into a Go time layout.
func StrftimeToGo(format string) string {
var out strings.Builder
for i := 0; i < len(format); {
if format[i] != '%' {
out.WriteByte(format[i])
i++
continue
}
matched := false
for _, r := range strftimeToGoRules {
if strings.HasPrefix(format[i:], r.strf) {
out.WriteString(r.goLayout)
i += len(r.strf)
matched = true
break
}
}
if !matched {
// Можно заменить на ошибку, если хочешь strict mode.
out.WriteByte(format[i])
i++
}
}
return out.String()
}
// GoToStrftime converts a Go time layout into an approximate strftime-like layout.
func GoToStrftime(format string) string {
format = strings.ReplaceAll(format, "2006", "%Y")
format = strings.ReplaceAll(format, "06", "%y")
format = strings.ReplaceAll(format, "15", "%H")
format = strings.ReplaceAll(format, "03", "%I")
format = strings.ReplaceAll(format, "3", "%-I")
format = strings.ReplaceAll(format, "3", "%#I")
format = strings.ReplaceAll(format, "January", "%B")
format = strings.ReplaceAll(format, "Jan", "%b")
format = strings.ReplaceAll(format, "01", "%m")
format = strings.ReplaceAll(format, "1", "%-m")
format = strings.ReplaceAll(format, "02", "%d")
format = strings.ReplaceAll(format, "_2", "%e")
format = strings.ReplaceAll(format, "2", "%-d")
format = strings.ReplaceAll(format, "Monday", "%A")
format = strings.ReplaceAll(format, "Mon", "%a")
format = strings.ReplaceAll(format, "04", "%M")
format = strings.ReplaceAll(format, "05", "%S")
format = strings.ReplaceAll(format, ".999999999", "%#N")
format = strings.ReplaceAll(format, "000000000", "%N")
format = strings.ReplaceAll(format, ".999999", "%#f")
format = strings.ReplaceAll(format, "000000", "%f")
format = strings.ReplaceAll(format, ".999", "%#L")
format = strings.ReplaceAll(format, "000", "%L")
format = strings.ReplaceAll(format, "PM", "%p")
format = strings.ReplaceAll(format, "-0700", "%z")
format = strings.ReplaceAll(format, "MST", "%Z")
format = strings.ReplaceAll(format, "Z07:00", "%:z")
format = strings.ReplaceAll(format, "Z07:00:00", "%::z")
format = strings.ReplaceAll(format, "Z07", "%:::z")
return format
}
+1 -1
View File
@@ -1,4 +1,4 @@
package slog package sneklog
import ( import (
"runtime" "runtime"
+1 -1
View File
@@ -1,4 +1,4 @@
package slog package sneklog
// Map applies f to each element of s and returns the resulting slice. // Map applies f to each element of s and returns the resulting slice.
func Map[T, R any](s []T, f func(T) R) []R { func Map[T, R any](s []T, f func(T) R) []R {
+79 -21
View File
@@ -1,4 +1,4 @@
package slog package sneklog
import ( import (
"encoding/json" "encoding/json"
@@ -17,13 +17,40 @@ type LoggerWriter interface {
Print(level LogLevel, prefix string, traceback []*MethodTraceback, messages ...any) error Print(level LogLevel, prefix string, traceback []*MethodTraceback, messages ...any) error
} }
// PrepareMessages normalizes message parts and extracts a trailing newline marker.
func PrepareMessages(messages ...any) (bool, []any) {
msg := Map(messages, func(el any) string { return fmt.Sprint(el) })
newline := false
if len(msg) > 0 && strings.HasSuffix(msg[len(msg)-1], "\n") {
newline = true
msg[len(msg)-1] = strings.TrimSuffix(msg[len(msg)-1], "\n")
if msg[len(msg)-1] == "" {
msg = msg[:len(msg)-1]
}
}
return newline, Map(msg, func(el string) any { return any(el) })
}
// LoggerTextWriter writes human-readable log records to an io.Writer. // LoggerTextWriter writes human-readable log records to an io.Writer.
type LoggerTextWriter struct { type LoggerTextWriter struct {
LoggerWriter LoggerWriter
writer io.Writer writer io.Writer
closer io.Closer closer io.Closer
printTraceback bool formatter *Formatter
printTime bool }
// SetFormatter replaces the formatter used by the text writer.
func (w *LoggerTextWriter) SetFormatter(formatter *Formatter) *LoggerTextWriter {
w.formatter = formatter
return w
}
// Formatter returns the effective formatter for the text writer.
func (w *LoggerTextWriter) Formatter() *Formatter {
if w.formatter == nil {
return DefaultTextFormatter
}
return w.formatter
} }
// Write forwards raw bytes to the underlying writer. // Write forwards raw bytes to the underlying writer.
@@ -32,11 +59,27 @@ func (w *LoggerTextWriter) Write(p []byte) (n int, err error) {
} }
// Print formats the provided record as text and writes it to the underlying writer. // Print formats the provided record as text and writes it to the underlying writer.
func (w *LoggerTextWriter) Print(level LogLevel, prefix string, _ []*MethodTraceback, messages ...any) error { func (w *LoggerTextWriter) Print(level LogLevel, prefix string, traceback []*MethodTraceback, messages ...any) error {
s := BuildString(level, prefix, w.printTime, w.printTraceback, messages...) newline, messages := PrepareMessages(messages...)
f := w.Formatter()
s := f.FormatMessage(level, prefix, traceback, messages...)
if f.ColorOutput && (!f.ColorOnlyStdout || w.writer == os.Stdout || w.writer == os.Stderr) {
s = f.ColorizeString(s, level)
}
_, err := w.Write([]byte(s)) _, err := w.Write([]byte(s))
if err != nil {
return err return err
} }
if newline {
_, err = w.Write([]byte("\n"))
if err != nil {
return err
}
}
return nil
}
// Close closes the owned writer, if any. // Close closes the owned writer, if any.
func (w *LoggerTextWriter) Close() error { func (w *LoggerTextWriter) Close() error {
@@ -52,14 +95,31 @@ type LoggerJsonWriter struct {
writer io.Writer writer io.Writer
closer io.Closer closer io.Closer
pretty bool pretty bool
formatter *Formatter
}
// Formatter returns the effective formatter for the JSON writer.
func (w *LoggerJsonWriter) Formatter() *Formatter {
if w.formatter == nil {
return DefaultJsonFormatter
}
return w.formatter
}
// SetFormatter replaces the formatter used by the JSON writer.
func (w *LoggerJsonWriter) SetFormatter(f *Formatter) *LoggerJsonWriter {
w.formatter = f
return w
} }
// LoggerJsonMessage is the JSON payload emitted by LoggerJsonWriter. // LoggerJsonMessage is the JSON payload emitted by LoggerJsonWriter.
type LoggerJsonMessage struct { type LoggerJsonMessage struct {
Time time.Time `json:"time"` Time string `json:"time"`
Level string `json:"level"` Level string `json:"level"`
Prefix string `json:"prefix"` Prefix string `json:"prefix"`
Message string `json:"message"` Message string `json:"message"`
Traceback []*MethodTraceback `json:"traceback"` Traceback []*MethodTraceback `json:"traceback"`
} }
@@ -70,21 +130,19 @@ func (w *LoggerJsonWriter) Write(data []byte) (int, error) {
// Print encodes the provided record as JSON and writes it to the underlying writer. // Print encodes the provided record as JSON and writes it to the underlying writer.
func (w *LoggerJsonWriter) Print(level LogLevel, prefix string, traceback []*MethodTraceback, messages ...any) error { func (w *LoggerJsonWriter) Print(level LogLevel, prefix string, traceback []*MethodTraceback, messages ...any) error {
msg := Map(messages, func(el any) string { newline, messages := PrepareMessages(messages...)
return fmt.Sprintf("%v", el)
})
newline := false f := w.Formatter()
if msg[len(msg)-1] == "\n" { s := f.FormatMessage(level, prefix, traceback, messages...)
newline = true if f.ColorOutput && (!f.ColorOnlyStdout || w.writer == os.Stdout || w.writer == os.Stderr) {
msg = msg[:len(msg)-1] s = f.ColorizeString(s, level)
} }
m := LoggerJsonMessage{ m := LoggerJsonMessage{
Time: time.Now(), Time: f.FormatTime(time.Now()),
Level: level.GetName(), Level: level.GetName(),
Prefix: prefix, Prefix: prefix,
Message: strings.TrimSpace(strings.Join(msg, " ")), Message: s,
Traceback: traceback, Traceback: traceback,
} }
var data []byte var data []byte
@@ -114,16 +172,16 @@ func (w *LoggerJsonWriter) Close() error {
// CreateTextWriter wraps an external writer for text output without taking // CreateTextWriter wraps an external writer for text output without taking
// ownership of it. // ownership of it.
func CreateTextWriter(w io.Writer, printTraceback, printTime bool) *LoggerTextWriter { func CreateTextWriter(w io.Writer) *LoggerTextWriter {
writer := &LoggerTextWriter{ writer := &LoggerTextWriter{
writer: w, printTraceback: printTraceback, printTime: printTime, writer: w,
} }
return writer return writer
} }
// CreateTextFileWriter creates a text writer for path, creating parent // CreateTextFileWriter creates a text writer for path, creating parent
// directories as needed. The returned writer owns the opened file. // directories as needed. The returned writer owns the opened file.
func CreateTextFileWriter(path string, printTraceback, printTime bool) (*LoggerTextWriter, error) { func CreateTextFileWriter(path string) (*LoggerTextWriter, error) {
err := os.MkdirAll(filepath.Dir(path), os.ModePerm) err := os.MkdirAll(filepath.Dir(path), os.ModePerm)
if err != nil { if err != nil {
return nil, err return nil, err
@@ -132,15 +190,15 @@ func CreateTextFileWriter(path string, printTraceback, printTime bool) (*LoggerT
if err != nil { if err != nil {
return nil, err return nil, err
} }
writer := CreateTextWriter(file, printTraceback, printTime) writer := CreateTextWriter(file)
writer.closer = file writer.closer = file
return writer, nil return writer, nil
} }
// CreateTextStdoutWriter creates a text writer for os.Stdout without taking // CreateTextStdoutWriter creates a text writer for os.Stdout without taking
// ownership of stdout. // ownership of stdout.
func CreateTextStdoutWriter(printTraceback, printTime bool) *LoggerTextWriter { func CreateTextStdoutWriter() *LoggerTextWriter {
writer := CreateTextWriter(os.Stdout, printTraceback, printTime) writer := CreateTextWriter(os.Stdout)
writer.closer = nil writer.closer = nil
return writer return writer
} }