Skip to content

Commit

Permalink
Merge pull request #42 from dl1998/add-wrapers-error-panic
Browse files Browse the repository at this point in the history
Add wrappers for error and panic
  • Loading branch information
dl1998 authored Apr 21, 2024
2 parents 3e95b05 e5803fb commit 2e265bb
Show file tree
Hide file tree
Showing 21 changed files with 4,766 additions and 3,258 deletions.
70 changes: 70 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 92,8 @@ All options available for the configuration are:

| Method | Default | Description |
|----------------|:-----------------------------:|------------------------------------------------------------------------------------|
| WithErrorLevel | level.Error | Set logging level used to log raised or captured error. |
| WithPanicLevel | level.Critical | Set logging level used to log panic. |
| WithFromLevel | level.Warning | Set logging level from which logger should log messages. |
| WithToLevel | level.Null | Set logging level till which logger should log messages. |
| WithTemplate | "%(level):%(name):%(message)" | Set template for logging message. |
Expand All @@ -103,6 105,8 @@ All options available for the configuration are:

| Method | Default | Description |
|-----------------------|:-------------------------------------------------------------------------------------------------------------------:|---------------------------------------------------------------------------------------------------------------------------------------|
| WithErrorLevel | level.Error | Set logging level used to log raised or captured error. |
| WithPanicLevel | level.Critical | Set logging level used to log panic. |
| WithFromLevel | level.Warning | Set logging level from which logger should log messages. |
| WithToLevel | level.Null | Set logging level till which logger should log messages. |
| WithTemplate | map[string]string {<br/>"timestamp": "%(timestamp)",<br/>"level": "%(level)",<br/>"name": "%(name)",<br/>} | Set template for logging structure. |
Expand Down Expand Up @@ -320,6 324,64 @@ if err := applicationLogger.Open(100); err != nil {
will add messages to the queue until it is not full, then it will wait (blocking the process) until the message from the
queue will be processed and free up the space in the message queue.*

### Error / Panic Wrapper

You could wrap error or raise a new error and log error message using the logger. By default, it will log error message
using the `level.Error` level. However, it could be changed by setting the error level in the logger configuration.

- Standard logger

```go
var err error
// Raise Error with default error level (level.Error)
err = applicationLogger.RaiseError("exit code: %d", 1)
// Change error level
applicationLogger.SetErrorLevel(level.Alert)
// Capture Error with new error level (level.Alert)
applicationLogger.CaptureError(err)
```

- Structured logger

```go
var err error
// Raise Error with default error level (level.Error) and additional fields
err = applicationLogger.RaiseError("exit code: 1", "hostname", "localhost")
// Change error level
applicationLogger.SetErrorLevel(level.Alert)
// Capture Error with new error level (level.Alert) and additional fields
applicationLogger.CaptureError(err, "hostname", "localhost")
```

Similarly, you could panic and log panic message using the logger. By default, it will log panic message using the
`level.Critical` level. However, it could be changed by setting the panic level in the logger configuration.

- Standard logger

```go
// Change panic level
applicationLogger.SetPanicLevel(level.Emergency)
// Raise Panic with new panic level (level.Emergency)
applicationLogger.Panic("exit code: %d", 1)
```

- Structured logger

```go
// Change panic level
applicationLogger.SetPanicLevel(level.Emergency)
// Raise Panic with new panic level (level.Emergency) and additional fields
applicationLogger.Panic("exit code: 1", "hostname", "localhost")
```

### Reading Configuration from File

You could also read configuration from a file. Configuration file should be in one of the following formats: `*.json`,
Expand All @@ -329,6 391,8 @@ You could also read configuration from a file. Configuration file should be in o
- Loggers (array of loggers)
- Name (string)
- Time Format (string)
- Error Level (string)
- Panic Level (string)
- Message Queue Size (int)
- Handlers (array of handlers)
- Type (string)
Expand All @@ -355,6 419,8 @@ Example of the configuration files:
{
"name": "example-logger",
"time-format": "2006-01-02 15:04:05",
"error-level": "error",
"panic-level": "critical",
"message-queue-size": 100,
"handlers": [
{
Expand Down Expand Up @@ -422,6 488,8 @@ Example of the configuration files:
loggers:
- name: example-logger
time-format: "2006-01-02 15:04:05"
error-level: error
panic-level: critical
message-queue-size: 100
handlers:
- type: stdout
Expand Down Expand Up @@ -472,6 540,8 @@ Example of the configuration files:
<logger>
<name>example-logger</name>
<time-format>2006-01-02 15:04:05</time-format>
<error-level>error</error-level>
<panic-level>critical</panic-level>
<message-queue-size>100</message-queue-size>
<handlers>
<handler>
Expand Down
54 changes: 54 additions & 0 deletions docs/architecture/diagrams/plantuml/class_diagram.plantuml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 34,8 @@ package pkg {
struct LoggerConfiguration {
Name : string
TimeFormat : string
ErrorLevel : string
PanicLevel : string
MessageQueueSize : int
Handlers : []HandlerConfiguration
}
Expand Down Expand Up @@ -282,9 284,18 @@ package pkg {
Alert(message : string, parameters : ...any)
Critical(message : string, parameters : ...any)
Emergency(message : string, parameters : ...any)
ErrorLevel() : level.Level
SetErrorLevel(newLevel : level.Level)
PanicLevel() : level.Level
SetPanicLevel(newLevel : level.Level)
RaiseError(message : string, parameters : ...any) : error
CaptureError(message : error)
Panic(message : string, parameters : ...any)
}
struct Logger implements Interface {
~ baseLogger : baseLoggerInterface
errorLevel : level.Level
panicLevel : level.Level
Name() : string
Handlers() : []handler.Interface
AddHandler(handlerInterface : handler.Interface)
Expand All @@ -300,6 311,13 @@ package pkg {
Alert(message : string, parameters : ...any)
Critical(message : string, parameters : ...any)
Emergency(message : string, parameters : ...any)
ErrorLevel() : level.Level
SetErrorLevel(newLevel : level.Level)
PanicLevel() : level.Level
SetPanicLevel(newLevel : level.Level)
RaiseError(message : string, parameters : ...any) : error
CaptureError(message : error)
Panic(message : string, parameters : ...any)
}
struct baseAsyncLogger implements baseLoggerInterface {
~ *baseLogger
Expand All @@ -325,6 343,8 @@ package pkg {
Close()
}
struct Configuration {
~ errorLevel : level.Level
~ panicLevel : level.Level
~ fromLevel : level.Level
~ toLevel : level.Level
~ template : string
Expand All @@ -341,6 361,8 @@ package pkg {
~ init()
New(name : string, timeFormat : string) : *Logger
NewAsyncLogger(name : string, timeFormat : string, queueSize : int) : *AsyncLogger
WithErrorLevel(errorLevel : level.Level) : Option
WithPanicLevel(panicLevel : level.Level) : Option
WithFromLevel(fromLevel : level.Level) : Option
WithToLevel(toLevel : level.Level) : Option
WithTemplate(template : string) : Option
Expand All @@ -364,6 386,13 @@ package pkg {
Alert(message : string, parameters : ...any)
Critical(message : string, parameters : ...any)
Emergency(message : string, parameters : ...any)
ErrorLevel() : level.Level
SetErrorLevel(newLevel : level.Level)
PanicLevel() : level.Level
SetPanicLevel(newLevel : level.Level)
RaiseError(message : string, parameters : ...any) : error
CaptureError(message : error)
Panic(message : string, parameters : ...any)
}

baseAsyncLogger *-- baseLogger
Expand Down Expand Up @@ -532,6 561,13 @@ package pkg {
Alert(parameters : ...any)
Critical(parameters : ...any)
Emergency(parameters : ...any)
ErrorLevel() : level.Level
SetErrorLevel(newLevel : level.Level)
PanicLevel() : level.Level
SetPanicLevel(newLevel : level.Level)
RaiseError(message : string, parameters : ...any) : error
CaptureError(message : error, parameters : ...any)
Panic(message : string, parameters : ...any)
}
struct Logger implements Interface {
~ baseLogger : baseLoggerInterface
Expand All @@ -550,6 586,13 @@ package pkg {
Alert(parameters : ...any)
Critical(parameters : ...any)
Emergency(parameters : ...any)
ErrorLevel() : level.Level
SetErrorLevel(newLevel : level.Level)
PanicLevel() : level.Level
SetPanicLevel(newLevel : level.Level)
RaiseError(message : string, parameters : ...any) : error
CaptureError(message : error, parameters : ...any)
Panic(message : string, parameters : ...any)
}
struct baseAsyncLogger implements baseLoggerInterface {
~ *baseLogger
Expand All @@ -575,6 618,8 @@ package pkg {
Close()
}
struct Configuration {
~ errorLevel : level.Level
~ panicLevel : level.Level
~ fromLevel : level.Level
~ toLevel : level.Level
~ template : map[string]string
Expand All @@ -595,6 640,8 @@ package pkg {
~ init()
New(name : string, timeFormat : string) : *Logger
NewAsyncLogger(name : string, timeFormat : string, queueSize : int) : *AsyncLogger
WithErrorLevel(errorLevel : level.Level) : Option
WithPanicLevel(panicLevel : level.Level) : Option
WithFromLevel(fromLevel : level.Level) : Option
WithToLevel(toLevel : level.Level) : Option
WithTemplate(template : map[string]string) : Option
Expand Down Expand Up @@ -622,6 669,13 @@ package pkg {
Alert(message : string, parameters : ...any)
Critical(message : string, parameters : ...any)
Emergency(message : string, parameters : ...any)
ErrorLevel() : level.Level
SetErrorLevel(newLevel : level.Level)
PanicLevel() : level.Level
SetPanicLevel(newLevel : level.Level)
RaiseError(message : string, parameters : ...any) : error
CaptureError(message : error, parameters : ...any)
Panic(message : string, parameters : ...any)
}

baseAsyncLogger *-- baseLogger
Expand Down
Binary file modified docs/architecture/diagrams/png/class_diagram.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 2e265bb

Please sign in to comment.