Skip to content

Commit

Permalink
[log] Add 'Divider' method
Browse files Browse the repository at this point in the history
  • Loading branch information
andyone committed Mar 3, 2024
1 parent c6ac5ee commit 7a658fb
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 38 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
### 12.102.0

* `[knf/validators/network]` Added `Mail` validator
* `[log]` Added `Divider` method
* `[knf/validators/fs]` Code refactoring
* `[knf/validators/network]` Code refactoring
* `[knf/validators/regexp]` Code refactoring
Expand Down
25 changes: 25 additions & 0 deletions log/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ func Example() {
// any minimum level
logger.Aux("This is aux message")

// Print simple divider
logger.Divider()

// For log rotation we provide method Reopen
logger.Reopen()

Expand Down Expand Up @@ -223,6 +226,17 @@ func ExampleAux() {
Aux("Auxiliary message")
}

func ExampleDivider() {
err := Set("/path/to/file.log", 0644)

if err != nil {
fmt.Printf("Error: %v\n", err)
return
}

Divider()
}

func ExampleIs() {
err := Set("/path/to/file.log", 0644)

Expand Down Expand Up @@ -391,6 +405,17 @@ func ExampleLogger_Aux() {
logger.Aux("Auxiliary message")
}

func ExampleLogger_Divider() {
logger, err := New("/path/to/file.log", 0644)

if err != nil {
fmt.Printf("Error: %v\n", err)
return
}

logger.Divider()
}

func ExampleLogger_Is() {
logger, err := New("/path/to/file.log", 0644)

Expand Down
14 changes: 14 additions & 0 deletions log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,11 @@ func Aux(f string, a ...any) error {
return Global.Aux(f, a...)
}

// Divider writes simple divider to logger output
func Divider() error {
return Global.Divider()
}

// Is returns true if current minimal logging level is equal or greater than
// given
func Is(level uint8) bool {
Expand Down Expand Up @@ -432,6 +437,15 @@ func (l *Logger) Aux(f string, a ...any) error {
return l.Print(AUX, f, a...)
}

// Divider writes simple divider to logger output
func (l *Logger) Divider() error {
if l == nil || l.mu == nil {
return ErrNilLogger
}

return l.Print(AUX, strings.Repeat("-", 80))
}

// Is returns true if current minimal logging level is equal or greater than
// given
func (l *Logger) Is(level uint8) bool {
Expand Down
88 changes: 50 additions & 38 deletions log/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ func (ls *LogSuite) TestErrors(c *C) {
c.Assert(err, NotNil)
c.Assert(err, DeepEquals, ErrNilLogger)

err = l.Divider()

c.Assert(err, NotNil)
c.Assert(err, DeepEquals, ErrNilLogger)

err = l.Set("", 0)

c.Assert(err, NotNil)
Expand Down Expand Up @@ -226,23 +231,25 @@ func (ls *LogSuite) TestWithoutPrefixes(c *C) {

c.Assert(fsutil.GetMode(logfile), Equals, os.FileMode(0644))

l.Print(DEBUG, "Test debug %d", DEBUG)
l.Print(INFO, "Test info %d", INFO)
l.Print(WARN, "Test warn %d", WARN)
l.Print(ERROR, "Test error %d", ERROR)
l.Print(CRIT, "Test crit %d", CRIT)
c.Assert(l.Print(DEBUG, "Test debug %d", DEBUG), IsNil)
c.Assert(l.Print(INFO, "Test info %d", INFO), IsNil)
c.Assert(l.Print(WARN, "Test warn %d", WARN), IsNil)
c.Assert(l.Print(ERROR, "Test error %d", ERROR), IsNil)
c.Assert(l.Print(CRIT, "Test crit %d", CRIT), IsNil)

l.Print(DEBUG, "Test debug")
l.Print(INFO, "Test info")
l.Print(WARN, "Test warn")
l.Print(ERROR, "Test error")
l.Print(CRIT, "Test crit")
c.Assert(l.Print(DEBUG, "Test debug"), IsNil)
c.Assert(l.Print(INFO, "Test info"), IsNil)
c.Assert(l.Print(WARN, "Test warn"), IsNil)
c.Assert(l.Print(ERROR, "Test error"), IsNil)
c.Assert(l.Print(CRIT, "Test crit"), IsNil)

l.Debug("Test debug %d\n", DEBUG)
l.Info("Test info %d\n", INFO)
l.Warn("Test warn %d\n", WARN)
l.Error("Test error %d\n", ERROR)
l.Crit("Test crit %d\n", CRIT)
c.Assert(l.Debug("Test debug %d\n", DEBUG), IsNil)
c.Assert(l.Info("Test info %d\n", INFO), IsNil)
c.Assert(l.Warn("Test warn %d\n", WARN), IsNil)
c.Assert(l.Error("Test error %d\n", ERROR), IsNil)
c.Assert(l.Crit("Test crit %d\n", CRIT), IsNil)

l.Divider()

l.Print(DEBUG, "")

Expand All @@ -253,7 +260,7 @@ func (ls *LogSuite) TestWithoutPrefixes(c *C) {

dataSlice := strings.Split(string(data), "\n")

c.Assert(len(dataSlice), Equals, 17)
c.Assert(len(dataSlice), Equals, 18)

c.Assert(dataSlice[0][28:], Equals, "Test debug 0")
c.Assert(dataSlice[1][28:], Equals, "Test info 1")
Expand All @@ -273,7 +280,9 @@ func (ls *LogSuite) TestWithoutPrefixes(c *C) {
c.Assert(dataSlice[13][28:], Equals, "Test error 3")
c.Assert(dataSlice[14][28:], Equals, "Test crit 4")

c.Assert(dataSlice[15][28:], Equals, "")
c.Assert(dataSlice[15][28:], Equals, "--------------------------------------------------------------------------------")

c.Assert(dataSlice[16][28:], Equals, "")
}

func (ls *LogSuite) TestWithPrefixes(c *C) {
Expand All @@ -294,26 +303,28 @@ func (ls *LogSuite) TestWithPrefixes(c *C) {

c.Assert(fsutil.GetMode(logfile), Equals, os.FileMode(0644))

Print(DEBUG, "Test debug %d", DEBUG)
Print(INFO, "Test info %d", INFO)
Print(WARN, "Test warn %d", WARN)
Print(ERROR, "Test error %d", ERROR)
Print(CRIT, "Test crit %d", CRIT)
Print(AUX, "Test aux %d", AUX)

Print(DEBUG, "Test debug")
Print(INFO, "Test info")
Print(WARN, "Test warn")
Print(ERROR, "Test error")
Print(CRIT, "Test crit")
Print(AUX, "Test aux")

Debug("Test debug %d", DEBUG)
Info("Test info %d", INFO)
Warn("Test warn %d", WARN)
Error("Test error %d", ERROR)
Crit("Test crit %d", CRIT)
Aux("Test aux %d", AUX)
c.Assert(Print(DEBUG, "Test debug %d", DEBUG), IsNil)
c.Assert(Print(INFO, "Test info %d", INFO), IsNil)
c.Assert(Print(WARN, "Test warn %d", WARN), IsNil)
c.Assert(Print(ERROR, "Test error %d", ERROR), IsNil)
c.Assert(Print(CRIT, "Test crit %d", CRIT), IsNil)
c.Assert(Print(AUX, "Test aux %d", AUX), IsNil)

c.Assert(Print(DEBUG, "Test debug"), IsNil)
c.Assert(Print(INFO, "Test info"), IsNil)
c.Assert(Print(WARN, "Test warn"), IsNil)
c.Assert(Print(ERROR, "Test error"), IsNil)
c.Assert(Print(CRIT, "Test crit"), IsNil)
c.Assert(Print(AUX, "Test aux"), IsNil)

c.Assert(Debug("Test debug %d", DEBUG), IsNil)
c.Assert(Info("Test info %d", INFO), IsNil)
c.Assert(Warn("Test warn %d", WARN), IsNil)
c.Assert(Error("Test error %d", ERROR), IsNil)
c.Assert(Crit("Test crit %d", CRIT), IsNil)
c.Assert(Aux("Test aux %d", AUX), IsNil)

c.Assert(Divider(), IsNil)

data, err := os.ReadFile(logfile)

Expand All @@ -322,7 +333,7 @@ func (ls *LogSuite) TestWithPrefixes(c *C) {

dataSlice := strings.Split(string(data), "\n")

c.Assert(len(dataSlice), Equals, 19)
c.Assert(len(dataSlice), Equals, 20)

c.Assert(dataSlice[0][28:], Equals, "[DEBUG] Test debug 0")
c.Assert(dataSlice[1][28:], Equals, "[INFO] Test info 1")
Expand All @@ -344,6 +355,7 @@ func (ls *LogSuite) TestWithPrefixes(c *C) {
c.Assert(dataSlice[15][28:], Equals, "[ERROR] Test error 3")
c.Assert(dataSlice[16][28:], Equals, "[CRITICAL] Test crit 4")
c.Assert(dataSlice[17][28:], Equals, "Test aux 99")
c.Assert(dataSlice[18][28:], Equals, "--------------------------------------------------------------------------------")
}

func (ls *LogSuite) TestBufIODaemon(c *C) {
Expand Down

0 comments on commit 7a658fb

Please sign in to comment.