|
| 1 | +# Upgrade Guide: Log v2 |
| 2 | + |
| 3 | +This guide covers everything you need to upgrade from Log v1 to v2. |
| 4 | + |
| 5 | +> [!TIP] |
| 6 | +> Most upgrades require just two changes: updating your import path and running `go get`. The API remains largely the same. |
| 7 | +
|
| 8 | +## Table of Contents |
| 9 | + |
| 10 | +- [Quick Start](#quick-start) |
| 11 | +- [Import Path Changes](#import-path-changes) |
| 12 | +- [Dependency Updates](#dependency-updates) |
| 13 | +- [Breaking Changes](#breaking-changes) |
| 14 | +- [Migration Checklist](#migration-checklist) |
| 15 | +- [Common Issues](#common-issues) |
| 16 | + |
| 17 | +## Quick Start |
| 18 | + |
| 19 | +For most projects, upgrading is straightforward: |
| 20 | + |
| 21 | +1. Update your import path |
| 22 | +2. Update dependencies |
| 23 | +3. Fix any type references (if you customize styles) |
| 24 | + |
| 25 | +**Estimated time:** 5-10 minutes for most codebases. |
| 26 | + |
| 27 | +## Import Path Changes |
| 28 | + |
| 29 | +The import path has changed to use the Charm vanity domain. |
| 30 | + |
| 31 | +### Before (v1) |
| 32 | + |
| 33 | +```go |
| 34 | +import "github.com/charmbracelet/log" |
| 35 | +``` |
| 36 | + |
| 37 | +### After (v2) |
| 38 | + |
| 39 | +```go |
| 40 | +import "charm.land/log/v2" |
| 41 | +``` |
| 42 | + |
| 43 | +**Update in your project:** |
| 44 | + |
| 45 | +```bash |
| 46 | +# Find all files that need updating |
| 47 | +grep -r "github.com/charmbracelet/log" . |
| 48 | + |
| 49 | +# Use your editor's find-and-replace to update imports |
| 50 | +# Or use a tool like gofmt with rewrites |
| 51 | +``` |
| 52 | + |
| 53 | +## Dependency Updates |
| 54 | + |
| 55 | +Update your `go.mod` file: |
| 56 | + |
| 57 | +```bash |
| 58 | +go get charm.land/log/v2@latest |
| 59 | +go mod tidy |
| 60 | +``` |
| 61 | + |
| 62 | +### New Dependencies |
| 63 | + |
| 64 | +Log v2 brings these updated dependencies: |
| 65 | + |
| 66 | +- **charm.land/lipgloss/v2** — Lip Gloss v2 for styling |
| 67 | +- **github.com/charmbracelet/colorprofile** — Replaces termenv for color profile detection |
| 68 | + |
| 69 | +### Removed Dependencies |
| 70 | + |
| 71 | +- **github.com/muesli/termenv** — No longer needed |
| 72 | +- **github.com/aymanbagabas/go-osc52/v2** — Removed (handled by Lip Gloss v2) |
| 73 | + |
| 74 | +## Breaking Changes |
| 75 | + |
| 76 | +### 1. Color Profile Type Change |
| 77 | + |
| 78 | +The `SetColorProfile` method now accepts `colorprofile.Profile` instead of `termenv.Profile`. |
| 79 | + |
| 80 | +#### Before (v1) |
| 81 | + |
| 82 | +```go |
| 83 | +import ( |
| 84 | + "github.com/charmbracelet/log" |
| 85 | + "github.com/muesli/termenv" |
| 86 | +) |
| 87 | + |
| 88 | +logger := log.New(os.Stderr) |
| 89 | +logger.SetColorProfile(termenv.TrueColor) |
| 90 | +``` |
| 91 | + |
| 92 | +#### After (v2) |
| 93 | + |
| 94 | +```go |
| 95 | +import ( |
| 96 | + "charm.land/log/v2" |
| 97 | + "github.com/charmbracelet/colorprofile" |
| 98 | +) |
| 99 | + |
| 100 | +logger := log.New(os.Stderr) |
| 101 | +logger.SetColorProfile(colorprofile.TrueColor) |
| 102 | +``` |
| 103 | + |
| 104 | +**Migration:** |
| 105 | + |
| 106 | +- Replace `termenv.Profile` imports with `colorprofile.Profile` |
| 107 | +- Update profile constants: |
| 108 | + - `termenv.TrueColor` → `colorprofile.TrueColor` |
| 109 | + - `termenv.ANSI256` → `colorprofile.ANSI256` |
| 110 | + - `termenv.ANSI` → `colorprofile.ANSI` |
| 111 | + - `termenv.Ascii` → `colorprofile.Ascii` |
| 112 | + - `termenv.NoTTY` → `colorprofile.NoTTY` |
| 113 | + |
| 114 | +### 2. Styles Type Changes |
| 115 | + |
| 116 | +All Lip Gloss style types in the `Styles` struct have changed to use Lip Gloss v2. |
| 117 | + |
| 118 | +#### Before (v1) |
| 119 | + |
| 120 | +```go |
| 121 | +import ( |
| 122 | + "github.com/charmbracelet/lipgloss" |
| 123 | + "github.com/charmbracelet/log" |
| 124 | +) |
| 125 | + |
| 126 | +styles := log.DefaultStyles() |
| 127 | +styles.Levels[log.ErrorLevel] = lipgloss.NewStyle(). |
| 128 | + Background(lipgloss.Color("204")) |
| 129 | +``` |
| 130 | + |
| 131 | +#### After (v2) |
| 132 | + |
| 133 | +```go |
| 134 | +import ( |
| 135 | + "charm.land/lipgloss/v2" |
| 136 | + "charm.land/log/v2" |
| 137 | +) |
| 138 | + |
| 139 | +styles := log.DefaultStyles() |
| 140 | +styles.Levels[log.ErrorLevel] = lipgloss.NewStyle(). |
| 141 | + Background(lipgloss.Color("204")) |
| 142 | +``` |
| 143 | + |
| 144 | +**Changed fields in `Styles` struct:** |
| 145 | + |
| 146 | +- `Caller` — Now `lipgloss/v2.Style` |
| 147 | +- `Key` — Now `lipgloss/v2.Style` |
| 148 | +- `Keys` — Now `map[string]lipgloss/v2.Style` |
| 149 | +- `Levels` — Now `map[Level]lipgloss/v2.Style` |
| 150 | +- `Message` — Now `lipgloss/v2.Style` |
| 151 | +- `Prefix` — Now `lipgloss/v2.Style` |
| 152 | +- `Separator` — Now `lipgloss/v2.Style` |
| 153 | +- `Timestamp` — Now `lipgloss/v2.Style` |
| 154 | +- `Value` — Now `lipgloss/v2.Style` |
| 155 | +- `Values` — Now `map[string]lipgloss/v2.Style` |
| 156 | + |
| 157 | +**Migration:** |
| 158 | + |
| 159 | +If you're using custom styles, update your Lip Gloss import: |
| 160 | + |
| 161 | +```go |
| 162 | +// Before |
| 163 | +import "github.com/charmbracelet/lipgloss" |
| 164 | + |
| 165 | +// After |
| 166 | +import "charm.land/lipgloss/v2" |
| 167 | +``` |
| 168 | + |
| 169 | +The Lip Gloss v2 API is mostly the same. See the [Lip Gloss v2 upgrade guide][lg-upgrade] for details on any style-specific changes. |
| 170 | + |
| 171 | +[lg-upgrade]: https://charm.land/lipgloss |
| 172 | + |
| 173 | +## Migration Checklist |
| 174 | + |
| 175 | +Use this checklist to ensure a smooth upgrade: |
| 176 | + |
| 177 | +- [ ] **Update import paths** from `github.com/charmbracelet/log` to `charm.land/log/v2` |
| 178 | +- [ ] **Update Lip Gloss imports** from `github.com/charmbracelet/lipgloss` to `charm.land/lipgloss/v2` (if using custom styles) |
| 179 | +- [ ] **Replace termenv usage** with `colorprofile` (if calling `SetColorProfile`) |
| 180 | +- [ ] **Run `go get charm.land/log/v2@latest`** |
| 181 | +- [ ] **Run `go mod tidy`** to clean up dependencies |
| 182 | +- [ ] **Build your project** with `go build` or `go test` |
| 183 | +- [ ] **Run your tests** to verify everything works |
| 184 | +- [ ] **Check your logs visually** in different terminals (optional but recommended) |
| 185 | + |
| 186 | +## Common Issues |
| 187 | + |
| 188 | +### Issue: "cannot find package" |
| 189 | + |
| 190 | +**Symptom:** |
| 191 | + |
| 192 | +``` |
| 193 | +cannot find package "github.com/charmbracelet/log" in any of: |
| 194 | +``` |
| 195 | + |
| 196 | +**Solution:** |
| 197 | + |
| 198 | +You missed updating an import path. Search your codebase: |
| 199 | + |
| 200 | +```bash |
| 201 | +grep -r "github.com/charmbracelet/log" . |
| 202 | +``` |
| 203 | + |
| 204 | +Update all occurrences to `charm.land/log/v2`. |
| 205 | + |
| 206 | +--- |
| 207 | + |
| 208 | +### Issue: "cannot use termenv.Profile as colorprofile.Profile" |
| 209 | + |
| 210 | +**Symptom:** |
| 211 | + |
| 212 | +``` |
| 213 | +cannot use termenv.TrueColor (type termenv.Profile) as type colorprofile.Profile |
| 214 | +``` |
| 215 | + |
| 216 | +**Solution:** |
| 217 | + |
| 218 | +Replace `termenv` imports and profile constants with `colorprofile`: |
| 219 | + |
| 220 | +```go |
| 221 | +// Before |
| 222 | +import "github.com/muesli/termenv" |
| 223 | +logger.SetColorProfile(termenv.TrueColor) |
| 224 | + |
| 225 | +// After |
| 226 | +import "github.com/charmbracelet/colorprofile" |
| 227 | +logger.SetColorProfile(colorprofile.TrueColor) |
| 228 | +``` |
| 229 | + |
| 230 | +--- |
| 231 | + |
| 232 | +### Issue: "lipgloss.Style type mismatch" |
| 233 | + |
| 234 | +**Symptom:** |
| 235 | + |
| 236 | +``` |
| 237 | +cannot use lipgloss.NewStyle() (type "github.com/charmbracelet/lipgloss".Style) |
| 238 | +as type "charm.land/lipgloss/v2".Style |
| 239 | +``` |
| 240 | + |
| 241 | +**Solution:** |
| 242 | + |
| 243 | +Update Lip Gloss imports to v2: |
| 244 | + |
| 245 | +```go |
| 246 | +// Before |
| 247 | +import "github.com/charmbracelet/lipgloss" |
| 248 | + |
| 249 | +// After |
| 250 | +import "charm.land/lipgloss/v2" |
| 251 | +``` |
| 252 | + |
| 253 | +--- |
| 254 | + |
| 255 | +### Issue: "module declares its path as X but was required as Y" |
| 256 | + |
| 257 | +**Symptom:** |
| 258 | + |
| 259 | +``` |
| 260 | +module declares its path as: charm.land/log/v2 |
| 261 | + but was required as: github.com/charmbracelet/log |
| 262 | +``` |
| 263 | + |
| 264 | +**Solution:** |
| 265 | + |
| 266 | +Run `go mod tidy` to sync your `go.mod` and `go.sum` files: |
| 267 | + |
| 268 | +```bash |
| 269 | +go mod tidy |
| 270 | +``` |
| 271 | + |
| 272 | +If the issue persists, clear your module cache: |
| 273 | + |
| 274 | +```bash |
| 275 | +go clean -modcache |
| 276 | +go mod tidy |
| 277 | +``` |
| 278 | + |
| 279 | +--- |
| 280 | + |
| 281 | +### Issue: Colors look wrong after upgrade |
| 282 | + |
| 283 | +**Symptom:** |
| 284 | + |
| 285 | +Logs display incorrectly or with garbled colors in some terminals. |
| 286 | + |
| 287 | +**Solution:** |
| 288 | + |
| 289 | +Log v2 automatically detects color profiles. If you were manually setting a profile, verify you're using the correct `colorprofile` constant: |
| 290 | + |
| 291 | +```go |
| 292 | +import "github.com/charmbracelet/colorprofile" |
| 293 | + |
| 294 | +// Explicitly set if needed |
| 295 | +logger.SetColorProfile(colorprofile.TrueColor) // or ANSI256, ANSI, etc. |
| 296 | +``` |
| 297 | + |
| 298 | +In most cases, you can remove manual profile setting—Log v2 handles detection automatically. |
| 299 | + |
| 300 | +--- |
| 301 | + |
| 302 | +## Need Help? |
| 303 | + |
| 304 | +If you run into issues not covered here: |
| 305 | + |
| 306 | +1. Check the [Log v2 release notes][release] |
| 307 | +2. Visit our [Discord community](https://charm.land/chat) |
| 308 | +3. Open an issue on [GitHub](https://github.com/charmbracelet/log/issues) |
| 309 | + |
| 310 | +We're here to help! |
| 311 | + |
| 312 | +[release]: https://github.com/charmbracelet/log/releases |
| 313 | + |
| 314 | +--- |
| 315 | + |
| 316 | +Part of [Charm](https://charm.land). |
| 317 | + |
| 318 | +<a href="https://charm.land/"><img alt="The Charm logo" src="https://stuff.charm.sh/charm-badge.jpg" width="400"></a> |
| 319 | + |
| 320 | +Charm热爱开源 • Charm loves open source • نحنُ نحب المصادر المفتوحة |
0 commit comments