Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagozs committed Feb 26, 2024
0 parents commit 5ed9a09
Show file tree
Hide file tree
Showing 12 changed files with 887 additions and 0 deletions.
Empty file added .gitignore
Empty file.
11 changes: 11 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Copyright 2024, Thiago Zilli Sarmento

All rights reserved. No permission is hereby granted to any person obtaining a copy of this software and associated documentation files. We reserve the rights to deal in the Software, to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software or to permit persons to whom the Software is furnished to do so.

In case you have our prior or written permission you are subject to the following conditions:

This copyright notice and permission notice shall be included in all copies or substantial portions of the Software.

If you begin patent litigation against the Licensor over patents that you think may apply to the software (including a cross-claim or counterclaim in a lawsuit), your license to the software ends automatically.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
100 changes: 100 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# logruswr - a Golang Logrus Wrapper

This wrapper enhances Logrus logging in Go applications, providing structured logging, customizable log levels, output formats, log rotation, and the ability to extend functionality with custom hooks.

## Features

- **Structured Logging**: Log messages with contextual information as key-value pairs.
- **Log Levels**: Control log output verbosity from Debug to Fatal.
- **Output Formats**: Choose between JSON and plain text for log output.
- **Log Rotation**: Automatically manage log file size and retention.
- **Custom Hooks**: Extend logging functionality by adding hooks.

## Installation

Use `go get` to install the Logrus wrapper:

```sh
go get github.com/thiagozs/go-logruswr
```
## Usage

### Basic Logging

Initialize the logger and log a simple message:

```go
package main

import (
"github.com/thiagozs/go-logruswr"
)

func main() {
logger, _ := logruswr.New()
logger.Info("Application started successfully")
}
```

### Structured Logging

Log messages with additional context:

```go
logger.WithFields(logruswr.Fields{"user_id": 42}).Info("User logged in")
```

### Error Logging

Log errors with contextual information:

```go
err := errors.New("failed to connect to database")
logger.WithError(err).Error("Database connection error")
```

### Log Rotation

Configure log rotation to manage log files:

```go
logger, _ := logruswr.New(
logruswr.WithLogFilePath("/var/log/myapp.log"),
logruswr.WithMaxLogSize(5), // in MB
logruswr.WithMaxBackups(3),
logruswr.WithMaxAge(7), // in days
logruswr.WithCompressLogs(true),
)
```

### Adding Custom Hooks

Extend logging functionality by implementing and adding custom hooks:

```go
type MyCustomHook struct {
// Custom hook implementation
}

func (hook *MyCustomHook) Levels() []logrus.Level {
return []logrus.Level{logrus.InfoLevel}
}

func (hook *MyCustomHook) Fire(entry *logrus.Entry) error {
// Hook logic here
return nil
}

// Usage
logger.AddHook(&MyCustomHook{})
```

With this setup, your application can leverage powerful logging capabilities with minimal effort. Customize the logger according to your needs, and take advantage of structured logging, log rotation, and extensibility with hooks.

-----

## Versioning and license

Our version numbers follow the [semantic versioning specification](http://semver.org/). You can see the available versions by checking the [tags on this repository](https://github.com/thiagozs/go-logruswr/tags). For more details about our license model, please take a look at the [LICENSE](LICENSE.md) file.

**2024**, thiagozs.
89 changes: 89 additions & 0 deletions consts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package logruswr

import "fmt"

const (
Panic Level = iota
Fatal
Error
Warn
Info
Debug
Trace

Stdout Console = iota
Stderr
File

FormatterText Formatter = iota
FormatterJSON
)

func (c Console) String() string {
switch c {
case Stdout:
return "stdout"
case Stderr:
return "stderr"
default:
return "stdout"
}
}

func (l Level) String() string {
switch l {
case Panic:
return "panic"
case Fatal:
return "fatal"
case Error:
return "error"
case Warn:
return "warn"
case Info:
return "info"
case Debug:
return "debug"
case Trace:
return "trace"
default:
return "info"
}
}

func (l Level) MarshalText() ([]byte, error) {
return []byte(l.String()), nil
}

func (l *Level) UnmarshalText(text []byte) error {
switch string(text) {
case "panic":
*l = Panic
case "fatal":
*l = Fatal
case "error":
*l = Error
case "warn":
*l = Warn
case "info":
*l = Info
case "debug":
*l = Debug
case "trace":
*l = Trace
default:
return fmt.Errorf("unknown level: %s", text)
}
return nil
}

func (f Formatter) String() string {
switch f {
case FormatterText:
return "text"
case FormatterJSON:
return "json"
default:
return "text"
}
}
25 changes: 25 additions & 0 deletions examples/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package main

import (
"fmt"

"github.com/thiagozs/go-logruswr"
)

func main() {
log, _ := logruswr.New()

log.Debug("Debug test")
log.Info("Info test")
log.Warn("Warning test")
log.Error("Error test")

log.WithError(fmt.Errorf("teste")).Info("Error with error test")

log.WithFields(logruswr.Fields{
"test": "test",
"test2": "test2",
}).Info("WithFields test")

log.Fatal("Fatal test")
}
10 changes: 10 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module github.com/thiagozs/go-logruswr

go 1.21.5

require github.com/sirupsen/logrus v1.9.3

require (
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
)
13 changes: 13 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST95x9zc=
gopkg.in/natefinch/lumberjack.v2 v2.2.1/go.mod h1:YD8tP3GAjkrDg1eZH7EGmyESg/lsYskCTPBJVb9jqSc=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
24 changes: 24 additions & 0 deletions hooks/testhook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package hooks

import "github.com/sirupsen/logrus"

type TestHook struct {
Fired bool
}

func (h *TestHook) Levels() []logrus.Level {
return logrus.AllLevels
}

func (h *TestHook) Fire(entry *logrus.Entry) error {
h.Fired = true
return nil
}

func (h *TestHook) Reset() {
h.Fired = false
}

func (h *TestHook) IsFired() bool {
return h.Fired
}
Loading

0 comments on commit 5ed9a09

Please sign in to comment.