1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
|
### logger/demo

<details>
<summary>SHOW SOURCE</summary>
```go
package main
import (
"github.com/pterm/pterm"
"time"
)
func main() {
// Create a logger with trace level
logger := pterm.DefaultLogger.WithLevel(pterm.LogLevelTrace)
// Log a trace level message
logger.Trace("Doing not so important stuff", logger.Args("priority", "super low"))
// Pause for 3 seconds
sleep()
// Define a map with interesting stuff
interstingStuff := map[string]any{
"when were crayons invented": "1903",
"what is the meaning of life": 42,
"is this interesting": true,
}
// Log a debug level message with arguments from the map
logger.Debug("This might be interesting", logger.ArgsFromMap(interstingStuff))
// Pause for 3 seconds
sleep()
// Log an info level message
logger.Info("That was actually interesting", logger.Args("such", "wow"))
// Pause for 3 seconds
sleep()
// Log a warning level message
logger.Warn("Oh no, I see an error coming to us!", logger.Args("speed", 88, "measures", "mph"))
// Pause for 3 seconds
sleep()
// Log an error level message
logger.Error("Damn, here it is!", logger.Args("error", "something went wrong"))
// Pause for 3 seconds
sleep()
// Log an info level message with a long text that will be automatically wrapped
logger.Info("But what's really cool is, that you can print very long logs, and PTerm will automatically wrap them for you! Say goodbye to text, that has weird line breaks!", logger.Args("very", "long"))
// Pause for 3 seconds
sleep()
// Log a fatal level message
logger.Fatal("Oh no, this process is getting killed!", logger.Args("fatal", true))
}
// Function to pause the execution for 3 seconds
func sleep() {
time.Sleep(time.Second * 3)
}
```
</details>
### logger/custom-key-styles

<details>
<summary>SHOW SOURCE</summary>
```go
package main
import "github.com/pterm/pterm"
func main() {
// Create a logger with a level of Trace or higher.
logger := pterm.DefaultLogger.WithLevel(pterm.LogLevelTrace)
// Define a new style for the "priority" key.
priorityStyle := map[string]pterm.Style{
"priority": *pterm.NewStyle(pterm.FgRed),
}
// Overwrite all key styles with the new map.
logger = logger.WithKeyStyles(priorityStyle)
// Log an info message. The "priority" key will be displayed in red.
logger.Info("The priority key should now be red", logger.Args("priority", "low", "foo", "bar"))
// Define a new style for the "foo" key.
fooStyle := *pterm.NewStyle(pterm.FgBlue)
// Append the new style to the existing ones.
logger.AppendKeyStyle("foo", fooStyle)
// Log another info message. The "foo" key will be displayed in blue.
logger.Info("The foo key should now be blue", logger.Args("priority", "low", "foo", "bar"))
}
```
</details>
### logger/default

<details>
<summary>SHOW SOURCE</summary>
```go
package main
import (
"github.com/pterm/pterm"
"time"
)
func main() {
// Create a logger with a level of Trace or higher.
logger := pterm.DefaultLogger.WithLevel(pterm.LogLevelTrace)
// Log a trace message with additional arguments.
logger.Trace("Doing not so important stuff", logger.Args("priority", "super low"))
// Create a map of interesting stuff.
interstingStuff := map[string]any{
"when were crayons invented": "1903",
"what is the meaning of life": 42,
"is this interesting": true,
}
// Log a debug message with arguments from a map.
logger.Debug("This might be interesting", logger.ArgsFromMap(interstingStuff))
// Log an info message with additional arguments.
logger.Info("That was actually interesting", logger.Args("such", "wow"))
// Log a warning message with additional arguments.
logger.Warn("Oh no, I see an error coming to us!", logger.Args("speed", 88, "measures", "mph"))
// Log an error message with additional arguments.
logger.Error("Damn, here it is!", logger.Args("error", "something went wrong"))
// Log an info message with additional arguments. PTerm will automatically wrap long logs.
logger.Info("But what's really cool is, that you can print very long logs, and PTerm will automatically wrap them for you! Say goodbye to text, that has weird line breaks!", logger.Args("very", "long"))
// Pause for 2 seconds.
time.Sleep(time.Second * 2)
// Log a fatal message with additional arguments. This will terminate the process.
logger.Fatal("Oh no, this process is getting killed!", logger.Args("fatal", true))
}
```
</details>
### logger/json

<details>
<summary>SHOW SOURCE</summary>
```go
package main
import "github.com/pterm/pterm"
func main() {
// Create a logger with Trace level and JSON formatter
logger := pterm.DefaultLogger.WithLevel(pterm.LogLevelTrace).WithFormatter(pterm.LogFormatterJSON)
// Log a Trace level message with additional arguments
logger.Trace("Doing not so important stuff", logger.Args("priority", "super low"))
// Create a map of interesting stuff
interestingStuff := map[string]any{
"when were crayons invented": "1903",
"what is the meaning of life": 42,
"is this interesting": true,
}
// Log a Debug level message with arguments from the map
logger.Debug("This might be interesting", logger.ArgsFromMap(interestingStuff))
// Log Info, Warn, Error, and Fatal level messages with additional arguments
logger.Info("That was actually interesting", logger.Args("such", "wow"))
logger.Warn("Oh no, I see an error coming to us!", logger.Args("speed", 88, "measures", "mph"))
logger.Error("Damn, here it is!", logger.Args("error", "something went wrong"))
logger.Info("But what's really cool is, that you can print very long logs, and PTerm will automatically wrap them for you! Say goodbye to text, that has weird line breaks!", logger.Args("very", "long"))
logger.Fatal("Oh no, this process is getting killed!", logger.Args("fatal", true))
}
```
</details>
### logger/with-caller

<details>
<summary>SHOW SOURCE</summary>
```go
package main
import "github.com/pterm/pterm"
func main() {
// Create a logger with Trace level and caller information
logger := pterm.DefaultLogger.WithLevel(pterm.LogLevelTrace).WithCaller()
// Log a trace message with additional arguments
logger.Trace("Doing not so important stuff", logger.Args("priority", "super low"))
// Create a map of interesting stuff
interestingStuff := map[string]any{
"when were crayons invented": "1903",
"what is the meaning of life": 42,
"is this interesting": true,
}
// Log a debug message with arguments from a map
logger.Debug("This might be interesting", logger.ArgsFromMap(interestingStuff))
// Log an info message with additional arguments
logger.Info("That was actually interesting", logger.Args("such", "wow"))
// Log a warning message with additional arguments
logger.Warn("Oh no, I see an error coming to us!", logger.Args("speed", 88, "measures", "mph"))
// Log an error message with additional arguments
logger.Error("Damn, here it is!", logger.Args("error", "something went wrong"))
// Log an info message with additional arguments. PTerm will automatically wrap long logs.
logger.Info("But what's really cool is, that you can print very long logs, and PTerm will automatically wrap them for you! Say goodbye to text, that has weird line breaks!", logger.Args("very", "long"))
// Log a fatal message with additional arguments. This will terminate the process.
logger.Fatal("Oh no, this process is getting killed!", logger.Args("fatal", true))
}
```
</details>
|