Skip to content

Commit

Permalink
[ADD] Option to silent empty log
Browse files Browse the repository at this point in the history
	SetNoEmpty(true)
  • Loading branch information
crownedgrouse committed Jun 3, 2023
1 parent 627cd79 commit eb3a144
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,13 @@ log.SetExitOnError(true) // Exit if log level reach Error or worst.
```
If the case, the error message is generated and a debug level may appear, depending current verbosity, indicating that an immediate exit occured, and telling what is the program exit code. The exit code is equal to the level reached by the last fatal error, i.e 1 (emergency) to 4 (error) , or even 5 if warning considered error.

Set option to silent empty log messages :

```go
slogan.SetNoEmpty(true) // Silent empty messages
```
Note: Exit on error is done even if message is empty and option set.

### Formats ###

Formats can be configured by settings new "Sprintf" values to the three arguments passed to `slogan` functions :
Expand Down
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
module github.com/crownedgrouse/slogan

go 1.12

require (
github.com/bclicn/color v0.0.0-20180711051946-108f2023dc84
golang.org/x/crypto v0.9.0
)
17 changes: 15 additions & 2 deletions slogan.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ var CallerBase bool = true
var Colorize bool = true
// should colorize even if output is not a terminal ?
var ForceColorize bool = false
// should empty log string logged ?
var NoEmpty bool = false

//************ Exported functions for configuration *************

Expand Down Expand Up @@ -161,6 +163,11 @@ func SetForceColor(mode bool) {
ForceColorize = mode
}

/**/
func SetNoEmpty(mode bool) {
NoEmpty = mode
}

/* Get color map */
func GetColors() map[int]string {
return colors
Expand Down Expand Up @@ -390,8 +397,14 @@ func decr_offset() {
// 1st argument is level integer, 2nd argument log string
func Log(level int, log string) {
if Verbosity >= level {
Str := logfmt(level, log)
logger.Println(Str)
allow := true
if NoEmpty == true && len(log) == 0 {
allow = false
}
if allow {
Str := logfmt(level, log)
logger.Println(Str)
}
}
if ((level < Lwarning) || (level == Lwarning && WarningAsError == true)) && (ExitOnError == true) {
incr_offset()
Expand Down

0 comments on commit eb3a144

Please sign in to comment.