Skip to content

Commit

Permalink
* Remove output parameter from NewLogger function
Browse files Browse the repository at this point in the history
* Implement SetOutput method
* Write log method's errors to internal error output
* Implement SetInternalErrorOutput method
* Improve synchronization
* Remove unnecessary methods
* Initialize go modules
* Update readme
* Format code
* Update tests
  • Loading branch information
ermanimer committed Nov 21, 2021
1 parent a1df8a6 commit 503f5f0
Show file tree
Hide file tree
Showing 5 changed files with 189 additions and 209 deletions.
5 changes: 1 addition & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,4 @@
*.out

# Dependency directories (remove the comment below to include it)
# vendor/

# GoLand
.idea/
# vendor/
86 changes: 57 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,35 +1,41 @@
# log

Simple, customizable, leveled and efficient logging in Go

[![Go](https://github.com/ermanimer/log/actions/workflows/go.yml/badge.svg?branch=main)](https://github.com/ermanimer/log/actions/workflows/go.yml) [![Go Report Card](https://goreportcard.com/badge/github.com/ermanimer/log)](https://goreportcard.com/report/github.com/ermanimer/log)

# Installation

```bash
go get -u github.com/ermanimer/log
```

# Features

**log** is a simple logging package for Go.

- You can set time format and prefixes.
- You can set logging level to filter out log messages.
- You can set a hook function to be called after each log.
- You can set output
- You can set internal error output (slog writes Log method's errors to internal error output instead of returning them.)
- You can set time format and prefixes
- You can set logging level to filter out log messages
- You can set a hook function to be called after each log

**log** isn't the fastest logging solution, but **log** is efficient while being customizable. Please see [benchmark tests](https://github.com/ermanimer/log#benchmark-tests).

# Usage

```go
package main

import (
"os"

"github.com/ermanimer/log"
"github.com/ermanimer/log/v2"
)

func main() {
// create a new logger instance with output and default parameters
l := log.NewLogger(os.Stdout)
l := log.NewLogger()

// log a message
l.Debug("this is a debug message")
Expand All @@ -40,36 +46,60 @@ func main() {
```

**Output:**

```bash
2021-06-07T16:46:26+03:00 debug this is a debug message
2021-06-07T16:46:26+03:00 debug this is a formatted debug message
```

# Logging Levels

- Debug
- Info
- Warning
- Error
- Fatal

# Default Parameters:

| Parameter | Value |
|:----------|:-----:|
| Time Format | RFC3339 |
| Debug Prefix | debug |
| Info Prefix | info |
| Warning Prefix | warning |
| Error Prefix | error |
| Fatal Prefix | fatal |
| Logging Level | Debug |
|Output|os.Stdout|
|Internal Error Output|os.Stderr|
|Time Format|RFC3339|
|Debug Prefix|debug|
|Info Prefix|info|
|Warning Prefix|warning|
|Error Prefix|error|
|Fatal Prefix|fatal|
|Logging Level|Debug|

# Set Output

```go
l.SetOutput(os.Stderr)
```

# Set Internal Error Output

```go
l.SetOutput(io.Discard)
```

# Set Logging Level

```go
l.SetLoggingLevel(InfoLevel)
```

# Set Time Format

```go
l.SetTimeFormat(time.RFC3339Nano)
```

# Set Prefixes

```go
l.SetDebugPrefix("DEB")
l.SetInfoPrefix("INF")
Expand All @@ -78,21 +108,18 @@ l.SetErrorPrefix("ERR")
l.SetFatalPrefix("FAT")
```

# Set Logging Level
```go
l.SetLoggingLevel(InfoLevel)
```

# Set Hook Function:

```go
l.SetHookFunction(func(prefix, message string) {
//filter messages with prefix and capture for Sentry.io
//...
//you can filter messages with prefix and capture for Sentry.io
})
```

# Benchmark Tests

**Test Codes:**

```go
func BenchmarkDebug(b *testing.B) {
// create logger
Expand All @@ -119,15 +146,16 @@ func BenchmarkDebugf(b *testing.B) {
}
```
**Results:**

| Function | Time | Bytes Allocated | Objects Allocated |
|:---------|:----:|:---------------:|:-----------------:|
| Debug | 410.7 ns/op | 4 B/op | 1 allocs/op |
| Debugf | 408.7 ns/op | 4 B/op | 1 allocs/op |
| Info | 404.0 ns/op | 4 B/op | 1 allocs/op |
| Infof | 403.9 ns/op | 4 B/op | 1 allocs/op |
| Warning | 407.0 ns/op | 4 B/op | 1 allocs/op |
| Warningf | 409.4 ns/op | 4 B/op | 1 allocs/op |
| Error | 404.6 ns/op | 4 B/op | 1 allocs/op |
| Errorf | 406.2 ns/op | 4 B/op | 1 allocs/op |
| Fatal | 402.1 ns/op | 4 B/op | 1 allocs/op |
| Fatalf | 406.0 ns/op | 4 B/op | 1 allocs/op |
|Debug|410.7 ns/op|4 B/op|1 allocs/op|
|Debugf|408.7 ns/op|4 B/op|1 allocs/op|
|Info|404.0 ns/op|4 B/op|1 allocs/op|
|Infof|403.9 ns/op|4 B/op|1 allocs/op|
|Warning|407.0 ns/op|4 B/op|1 allocs/op|
|Warningf|409.4 ns/op|4 B/op|1 allocs/op|
|Error|404.6 ns/op|4 B/op|1 allocs/op|
|Errorf|406.2 ns/op|4 B/op|1 allocs/op|
|Fatal|402.1 ns/op |4 B/op|1 allocs/op|
|Fatalf|406.0 ns/op|4 B/op|1 allocs/op|
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/ermanimer/log/v2

go 1.17
Loading

0 comments on commit 503f5f0

Please sign in to comment.