|
| 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 | + |
| 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` |
0 commit comments