-
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.
Merge pull request #2 from n0rdy/feature/logging
Implemented logging
- Loading branch information
Showing
16 changed files
with
644 additions
and
33 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
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,68 @@ | ||
package logging | ||
|
||
import ( | ||
"fmt" | ||
"github.com/n0rdy/pippin/types/loglevels" | ||
"github.com/n0rdy/pippin/utils" | ||
) | ||
|
||
// ChannelLogger is a logger that writes to a channel. | ||
// Please, make sure that you read from the channel, otherwise it will block the pipeline execution. | ||
// | ||
// ChannelLogger is handy when you want to write logs to a file or do some heavy actions on them, but don't want to affect performance of the pipeline. | ||
// | ||
// ChannelLogger accepts a log level as a parameter. | ||
// It will print only those logs that have a level equal or higher than the specified one. | ||
// Check [loglevels.LogLevel] for more details. | ||
// | ||
// Make sure to call the ChannelLogger.Close() method when you are done with the logger - this will close the channel. | ||
type ChannelLogger struct { | ||
ch chan<- string | ||
level loglevels.LogLevel | ||
} | ||
|
||
func NewChannelLogger(ch chan<- string, level loglevels.LogLevel) Logger { | ||
return &ChannelLogger{ | ||
ch: ch, | ||
level: level, | ||
} | ||
} | ||
|
||
func (cl *ChannelLogger) Trace(message string) { | ||
if cl.level <= loglevels.TRACE { | ||
cl.ch <- utils.TimeNowAsRFC3339NanoString() + loglevels.TracePrefix + message | ||
} | ||
} | ||
|
||
func (cl *ChannelLogger) Debug(message string) { | ||
if cl.level <= loglevels.DEBUG { | ||
cl.ch <- utils.TimeNowAsRFC3339NanoString() + loglevels.DebugPrefix + message | ||
} | ||
} | ||
|
||
func (cl *ChannelLogger) Info(message string) { | ||
if cl.level <= loglevels.INFO { | ||
cl.ch <- utils.TimeNowAsRFC3339NanoString() + loglevels.InfoPrefix + message | ||
} | ||
} | ||
|
||
func (cl *ChannelLogger) Warn(message string, errs ...error) { | ||
if cl.level <= loglevels.WARN { | ||
cl.ch <- utils.TimeNowAsRFC3339NanoString() + loglevels.WarnPrefix + cl.messageWithErrors(message, errs...) | ||
} | ||
} | ||
|
||
func (cl *ChannelLogger) Error(message string, errs ...error) { | ||
if cl.level <= loglevels.ERROR { | ||
cl.ch <- utils.TimeNowAsRFC3339NanoString() + loglevels.ErrorPrefix + cl.messageWithErrors(message, errs...) | ||
} | ||
} | ||
|
||
func (cl *ChannelLogger) Close() error { | ||
close(cl.ch) | ||
return nil | ||
} | ||
|
||
func (cl *ChannelLogger) messageWithErrors(message string, errs ...error) string { | ||
return fmt.Sprintf(message, errs) | ||
} |
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,61 @@ | ||
package logging | ||
|
||
import ( | ||
"fmt" | ||
"github.com/n0rdy/pippin/types/loglevels" | ||
"github.com/n0rdy/pippin/utils" | ||
) | ||
|
||
// ConsoleLogger is a logger that prints logs to console. | ||
// Basically, it uses fmt.Println() to print logs under the hood. | ||
// | ||
// ConsoleLogger accepts a log level as a parameter. | ||
// It will print only those logs that have a level equal or higher than the specified one. | ||
// Check [loglevels.LogLevel] for more details. | ||
// | ||
// The format of logs is: "time [log level] message" | ||
// Example: | ||
// 2006-01-02 15:04:05:000 [INFO] some cool info message | ||
type ConsoleLogger struct { | ||
level loglevels.LogLevel | ||
} | ||
|
||
func NewConsoleLogger(level loglevels.LogLevel) Logger { | ||
return &ConsoleLogger{ | ||
level: level, | ||
} | ||
} | ||
|
||
func (cl *ConsoleLogger) Trace(message string) { | ||
if cl.level <= loglevels.TRACE { | ||
fmt.Println(utils.TimeNowAsRFC3339NanoString() + loglevels.TracePrefix + message) | ||
} | ||
} | ||
|
||
func (cl *ConsoleLogger) Debug(message string) { | ||
if cl.level <= loglevels.DEBUG { | ||
fmt.Println(utils.TimeNowAsRFC3339NanoString() + loglevels.DebugPrefix + message) | ||
} | ||
} | ||
|
||
func (cl *ConsoleLogger) Info(message string) { | ||
if cl.level <= loglevels.INFO { | ||
fmt.Println(utils.TimeNowAsRFC3339NanoString() + loglevels.InfoPrefix + message) | ||
} | ||
} | ||
|
||
func (cl *ConsoleLogger) Warn(message string, errs ...error) { | ||
if cl.level <= loglevels.WARN { | ||
fmt.Println(utils.TimeNowAsRFC3339NanoString()+loglevels.WarnPrefix+message, errs) | ||
} | ||
} | ||
|
||
func (cl *ConsoleLogger) Error(message string, errs ...error) { | ||
if cl.level <= loglevels.ERROR { | ||
fmt.Println(utils.TimeNowAsRFC3339NanoString()+loglevels.ErrorPrefix+message, errs) | ||
} | ||
} | ||
|
||
func (cl *ConsoleLogger) Close() error { | ||
return nil | ||
} |
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,10 @@ | ||
package logging | ||
|
||
type Logger interface { | ||
Trace(message string) | ||
Debug(message string) | ||
Info(message string) | ||
Warn(message string, errs ...error) | ||
Error(message string, errs ...error) | ||
Close() error | ||
} |
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,23 @@ | ||
package logging | ||
|
||
// NoOpsLogger is a logger that does nothing. | ||
// This is a default logger for the service if no logger is configured. | ||
type NoOpsLogger struct{} | ||
|
||
func NewNoOpsLogger() Logger { | ||
return &NoOpsLogger{} | ||
} | ||
|
||
func (nol *NoOpsLogger) Trace(message string) {} | ||
|
||
func (nol *NoOpsLogger) Debug(message string) {} | ||
|
||
func (nol *NoOpsLogger) Info(message string) {} | ||
|
||
func (nol *NoOpsLogger) Warn(message string, errs ...error) {} | ||
|
||
func (nol *NoOpsLogger) Error(message string, errs ...error) {} | ||
|
||
func (nol *NoOpsLogger) Close() error { | ||
return nil | ||
} |
Oops, something went wrong.