Skip to content

Commit 84ef6c7

Browse files
committed
init
0 parents  commit 84ef6c7

23 files changed

Lines changed: 3463 additions & 0 deletions

File tree

.github/workflows/ci.yml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
9+
jobs:
10+
build-and-test:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- name: Checkout code
14+
uses: actions/checkout@v4
15+
16+
- name: Set up Go
17+
uses: actions/setup-go@v5
18+
with:
19+
go-version: '1.25'
20+
cache: true
21+
22+
- name: Format Check
23+
run: |
24+
if [ -n "$(gofmt -l .)" ]; then
25+
echo "The following files are not formatted:"
26+
gofmt -l .
27+
exit 1
28+
fi
29+
30+
- name: Run Linter
31+
uses: golangci/golangci-lint-action@v6
32+
with:
33+
version: v1.64
34+
args: --timeout=5m
35+
36+
- name: Build
37+
run: go build -v ./...
38+
39+
- name: Run Tests
40+
run: go test -v -race ./...

.gitignore

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# --- Go specific ---
2+
bin/
3+
pretty-go-log
4+
*.test
5+
*.out
6+
vendor/
7+
8+
# --- Project specific ---
9+
**/logs/
10+
*.log
11+
logrus/logs/
12+
13+
# --- OS / IDE specific ---
14+
.DS_Store
15+
.idea/
16+
.vscode/
17+
*.swp
18+
.cache/

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2026 canefe
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
<div align="center" style="margin-bottom: 1rem;">
2+
<h1 style="font-size: 2rem;margin: 0;"> pretty-go-log </h1>
3+
4+
<a href="https://github.com/canefe/pretty-go-log/releases">
5+
<img src="https://img.shields.io/github/v/release/canefe/pretty-go-log?include_prereleases" alt="Latest Version" />
6+
</a>
7+
<a href="https://github.com/canefe/pretty-go-log/actions">
8+
<img src="https://github.com/canefe/pretty-go-log/actions/workflows/go.yml/badge.svg" alt="Build Status" />
9+
</a>
10+
<a href="https://goreportcard.com/report/github.com/canefe/pretty-go-log">
11+
<img src="https://goreportcard.com/badge/github.com/canefe/pretty-go-log" alt="Go Report Card" />
12+
</a>
13+
14+
<br />
15+
16+
<img src="https://img.shields.io/badge/go-1.25.5-00ADD8.svg" alt="Go Version" />
17+
<img src="https://img.shields.io/badge/logrus-1.9.3-blue.svg" alt="Logrus" />
18+
<img src="https://img.shields.io/badge/output-console%20%7C%20file%20%7C%20multi-brightgreen.svg" alt="Outputs" />
19+
</div>
20+
21+
A small Go package that gives logrus a cleaner, structured, and visually aligned output style with bracketed tags, optional colors, and multi-output support.
22+
23+
Use it as a drop-in logger with sane defaults, or fully customize formatting, output, and caller behavior using functional options.
24+
25+
![pretty-go-log](https://i.imgur.com/zK3tD3M.png)
26+
27+
## What is Pretty Go Log?
28+
29+
Pretty Go Log wraps logrus with a formatter designed for readability:
30+
31+
- **Aligned Tag Output**. Bracketed tags align to a common column for fast scanning
32+
- **Multiple Tag Styles**. Default, centered, or right-aligned tag padding
33+
- **Multi Output**. Console + rotating file output with separate formatters
34+
- **Configurable Colors**. Colorized levels and tags, dim gray padding
35+
- **Caller Awareness**. Toggle caller output for warnings and errors
36+
- **Environment Config**. Use `LOG_LEVEL`, `LOG_OUTPUT`, `LOG_FORMAT` defaults
37+
38+
## Installation
39+
40+
```bash
41+
go get github.com/canefe/pretty-go-log
42+
```
43+
44+
## Quick Start
45+
46+
```go
47+
package main
48+
49+
import (
50+
"github.com/canefe/pretty-go-log/logrus/pretty"
51+
)
52+
53+
func main() {
54+
log := pretty.New()
55+
56+
log.Info("[Server] Started on :8080")
57+
log.Warn("[Cache] Miss for key user:123")
58+
}
59+
```
60+
61+
## Usage
62+
63+
### Functional Options
64+
65+
```go
66+
package main
67+
68+
import (
69+
"github.com/canefe/pretty-go-log/logrus/pretty"
70+
"github.com/sirupsen/logrus"
71+
)
72+
73+
func main() {
74+
log := pretty.New(
75+
pretty.WithLevel(logrus.DebugLevel),
76+
pretty.WithOutput(pretty.OutputMulti),
77+
pretty.WithFormat(pretty.FormatPlain),
78+
pretty.WithNamespace("App"),
79+
pretty.WithFile("logs/service.log"),
80+
)
81+
82+
log.Debug("[Init] Starting")
83+
}
84+
```
85+
86+
### Custom Formatter
87+
88+
```go
89+
package main
90+
91+
import (
92+
"github.com/canefe/pretty-go-log/logrus/pretty"
93+
"github.com/sirupsen/logrus"
94+
)
95+
96+
func main() {
97+
formatter := pretty.NewCustomFormatter(
98+
pretty.WithTagStyle(pretty.StyleRight, "."),
99+
pretty.WithBracketPadding(15),
100+
pretty.WithColorBrackets(true),
101+
)
102+
103+
log := pretty.New(
104+
pretty.WithLevel(logrus.InfoLevel),
105+
pretty.WithOutput(pretty.OutputMulti),
106+
pretty.WithCustomFormat(*formatter),
107+
pretty.WithFile("logs/service.log"),
108+
)
109+
110+
log.Info("[API] Request completed")
111+
}
112+
```
113+
114+
### Environment Configuration
115+
116+
```go
117+
package main
118+
119+
import (
120+
"os"
121+
122+
"github.com/canefe/pretty-go-log/logrus/pretty"
123+
)
124+
125+
func main() {
126+
os.Setenv("LOG_LEVEL", "debug")
127+
os.Setenv("LOG_OUTPUT", "console")
128+
os.Setenv("LOG_FORMAT", "plain")
129+
130+
log := pretty.New()
131+
log.Debug("[Env] Logger configured via env vars")
132+
}
133+
```
134+
135+
## Options and Types
136+
137+
### Output Types
138+
139+
- `pretty.OutputConsole`
140+
- `pretty.OutputFile`
141+
- `pretty.OutputMulti`
142+
143+
### Format Types
144+
145+
- `pretty.FormatRaw`
146+
- `pretty.FormatPlain`
147+
- `pretty.FormatJSON`
148+
149+
### Common Options
150+
151+
- `pretty.WithLevel(level logrus.Level)`
152+
- `pretty.WithOutput(output pretty.OutputType)`
153+
- `pretty.WithFormat(format pretty.FormatType)`
154+
- `pretty.WithNamespace(name string)`
155+
- `pretty.WithFile(path string)`
156+
- `pretty.WithoutCaller()`
157+
- `pretty.WithCustomFormat(formatter pretty.CustomFormatter)`
158+
159+
## Examples
160+
161+
- `examples/logrus/basic`
162+
- `examples/logrus/custom-config`
163+
- `examples/logrus/env-config`
164+
- `examples/logrus/file-output`
165+
- `examples/logrus/json-format`
166+
- `examples/logrus/multi-output`
167+
- `examples/showcase`

examples/logrus/basic/main.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package main
2+
3+
import (
4+
"github.com/canefe/pretty-go-log/logrus/pretty"
5+
)
6+
7+
func main() {
8+
// Create a new logger with default settings
9+
log := pretty.New()
10+
11+
// Log messages at different levels
12+
log.Debug("This is a debug message")
13+
log.Info("Application started successfully")
14+
log.Info("[Server] Listening on port 8080")
15+
log.Warn("This is a warning message")
16+
log.Error("This is an error message")
17+
log.Trace("hey")
18+
19+
// With fields - fields appear in dim color at the end of the line
20+
log.WithField("user", "alice").Info("User logged in")
21+
log.WithField("duration", "150ms").Info("[Request] GET /api/users")
22+
23+
// Multiple fields - automatically sorted alphabetically
24+
log.WithFields(map[string]interface{}{
25+
"user_id": 123,
26+
"endpoint": "/api/data",
27+
"method": "POST",
28+
"status": 200,
29+
}).Info("[API] Request completed")
30+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package main
2+
3+
import (
4+
"github.com/canefe/pretty-go-log/logrus/pretty"
5+
"github.com/sirupsen/logrus"
6+
)
7+
8+
func main() {
9+
// Create a logger with custom configuration
10+
log := pretty.New(
11+
pretty.WithLevel(logrus.DebugLevel),
12+
pretty.WithOutput(pretty.OutputConsole),
13+
pretty.WithFormat(pretty.FormatPlain),
14+
)
15+
16+
log.Debug("[Init] Initializing application")
17+
log.Info("[Config] Configuration loaded")
18+
log.Info("[Database] Connected to database")
19+
log.Warn("[Cache] Cache miss for key: user:123")
20+
log.Error("[API] Failed to connect to external service")
21+
22+
// Some other logger
23+
ordersLog := pretty.New(
24+
pretty.WithLevel(logrus.DebugLevel),
25+
pretty.WithOutput(pretty.OutputMulti), // multi
26+
pretty.WithFormat(pretty.FormatPlain),
27+
pretty.WithNamespace("Orders"),
28+
pretty.WithFile("logs/orders.log"),
29+
)
30+
31+
ordersLog.WithFields(logrus.Fields{
32+
"id": "123",
33+
"userId": "12345",
34+
}).Info("[Order] New order!")
35+
}

examples/logrus/env-config/main.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package main
2+
3+
import (
4+
"os"
5+
6+
"github.com/canefe/pretty-go-log/logrus/pretty"
7+
)
8+
9+
func main() {
10+
// The logger can be configured via environment variables
11+
// Set these before running:
12+
// export LOG_LEVEL=debug
13+
// export LOG_OUTPUT=console
14+
// export LOG_FORMAT=plain
15+
16+
// When options are omitted, the logger will
17+
// check the corresponding environment variables
18+
os.Setenv("LOG_LEVEL", "debug")
19+
os.Setenv("LOG_OUTPUT", "console")
20+
os.Setenv("LOG_FORMAT", "plain")
21+
22+
log := pretty.New()
23+
24+
log.Debug("[Env] Logger configured via environment variables")
25+
log.Info("[Env] LOG_LEVEL=" + os.Getenv("LOG_LEVEL"))
26+
log.Info("[Env] LOG_OUTPUT=" + os.Getenv("LOG_OUTPUT"))
27+
log.Info("[Env] LOG_FORMAT=" + os.Getenv("LOG_FORMAT"))
28+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package main
2+
3+
import (
4+
"github.com/canefe/pretty-go-log/logrus/pretty"
5+
"github.com/sirupsen/logrus"
6+
)
7+
8+
func main() {
9+
// Create a logger that writes to a file
10+
// The file will be rotated automatically using lumberjack
11+
log := pretty.New(
12+
pretty.WithLevel(logrus.InfoLevel),
13+
pretty.WithOutput(pretty.OutputFile), // Output to file (logs/service.log by default)
14+
pretty.WithFormat(pretty.FormatPlain),
15+
pretty.WithFile("logs/service.log"),
16+
pretty.WithoutCaller(),
17+
)
18+
19+
log.Info("Application started - this will be written to file")
20+
log.Info("[Server] Server is running")
21+
22+
for i := 0; i < 10; i++ {
23+
log.WithField("iteration", i).Info("[Task] Processing item")
24+
}
25+
26+
log.Info("Application finished - check logs/service.log")
27+
}

0 commit comments

Comments
 (0)