-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Color output on console * fix the tests, change text format to escape value but not key, same in color mode * add screenshot/example * add json and makefile showing both json vs color with same sample binary * update text and screenshot * add color tests * update depguard + lint fix * compactify * fancier attribute key value output * update test too * add comments for exposed functions
- Loading branch information
Showing
8 changed files
with
234 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
|
||
all: test example | ||
|
||
test: | ||
go test . -race ./... | ||
|
||
example: | ||
@echo "### Colorized (default) ###" | ||
go run ./levelsDemo | ||
@echo "### JSON: (redirected stderr) ###" | ||
go run ./levelsDemo 3>&1 1>&2 2>&3 | jq -c | ||
|
||
line: | ||
@echo | ||
|
||
# Suitable to make a screenshot with a bit of spaces around for updating color.png | ||
screenshot: line example | ||
@echo | ||
|
||
.PHONY: all test example screenshot line |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// Copyright 2023 Fortio Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package log | ||
|
||
import ( | ||
"os" | ||
"time" | ||
) | ||
|
||
const ( | ||
// ANSI color codes. | ||
reset = "\033[0m" | ||
red = "\033[31m" | ||
green = "\033[32m" | ||
yellow = "\033[33m" | ||
blue = "\033[34m" | ||
purple = "\033[35m" | ||
cyan = "\033[36m" | ||
gray = "\033[37m" | ||
white = "\033[97m" | ||
brightRed = "\033[91m" | ||
darkGray = "\033[90m" | ||
) | ||
|
||
var ( | ||
// Mapping of log levels to color. | ||
LevelToColor = []string{ | ||
gray, | ||
cyan, | ||
green, | ||
yellow, | ||
red, | ||
purple, | ||
brightRed, | ||
} | ||
// Cached flag for whether to use color output or not. | ||
Color = false | ||
) | ||
|
||
// ConsoleLogging is a utility to check if the current logger output is a console (terminal). | ||
func ConsoleLogging() bool { | ||
f, ok := jsonWriter.(*os.File) | ||
if !ok { | ||
return false | ||
} | ||
s, _ := f.Stat() | ||
return (s.Mode() & os.ModeCharDevice) == os.ModeCharDevice | ||
} | ||
|
||
// SetColorMode computes whether we currently should be using color text mode or not. | ||
// Need to be reset if config changes (but is already automatically re evaluated when calling SetOutput()). | ||
func SetColorMode() { | ||
Color = ColorMode() | ||
} | ||
|
||
// ColorMode returns true if we should be using color text mode, which is either because it's | ||
// forced or because we are in a console and the config allows it. | ||
// Should not be called often, instead read/update the Color variable when needed. | ||
func ColorMode() bool { | ||
return Config.ForceColor || (Config.ConsoleColor && ConsoleLogging()) | ||
} | ||
|
||
func colorTimestamp() string { | ||
if Config.NoTimestamp { | ||
return "" | ||
} | ||
return time.Now().Format("\033[90m15:04:05.000 ") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Initially from https://github.com/fortio/logc/blob/v1.1.0/levelsDemo/levels.go | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
"fortio.org/log" | ||
) | ||
|
||
func main() { | ||
// So log fatal doesn't panic nor exit (so we can print the non json last line). | ||
log.Config.FatalPanics = false | ||
log.Config.FatalExit = func(int) {} | ||
log.SetLogLevelQuiet(log.Debug) | ||
// Meat of the example: (some of these are reproducing fixed issues in `logc` json->console attributes detection) | ||
log.Debugf("This is a debug message ending with backslash \\") | ||
log.LogVf("This is a verbose message") | ||
log.Printf("This an always printed, file:line omitted message") | ||
log.Infof("This is an info message with no attributes but with \"quotes\"...") | ||
log.S(log.Info, "This is multi line\n\tstructured info message with 3 attributes", | ||
log.Str("attr1", "value1"), log.Attr("attr2", 42), log.Str("attr3", "\"quoted\nvalue\"")) | ||
log.Warnf("This is a warning message") | ||
log.Errf("This is an error message") | ||
log.Critf("This is a critical message") | ||
log.Fatalf("This is a fatal message") //nolint:revive // we disabled exit for this demo | ||
fmt.Println("This is a non json output, will get prefixed with a exclamation point with logc") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters