first commit
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2025 KORNET
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
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.
|
||||
@@ -0,0 +1,49 @@
|
||||
# Rotate Hook
|
||||
|
||||
This is a simple hook for logrus [logrus](https://git.corp.kornet35.ru/gopkg/logrus) to write log files using [gopkg/lumberjack](https://git.corp.kornet35.ru/gopkg/lumberjack)
|
||||
|
||||
## Usage
|
||||
|
||||
```go
|
||||
import (
|
||||
"git.corp.kornet35.ru/gopkg/logrus"
|
||||
"git.corp.kornet35.ru/gopkg/nested"
|
||||
"git.corp.kornet35.ru/gopkg/rotatehook"
|
||||
"github.com/mattn/go-colorable"
|
||||
)
|
||||
|
||||
log := &logrus.Logger{
|
||||
Out: colorable.NewColorableStdout(),
|
||||
Formatter: &nested.Formatter{
|
||||
TimestampFormat: "2006-01-02 15:04:05",
|
||||
HideKeys: true,
|
||||
},
|
||||
Hooks: make(logrus.LevelHooks),
|
||||
Level: logrus.InfoLevel,
|
||||
}
|
||||
|
||||
hook := rotatehook.NewRotateHook(&rotatehook.Config{
|
||||
Filename: "debug.log",
|
||||
MaxSize: 5,
|
||||
MaxAge: 30,
|
||||
MaxBackups: 10,
|
||||
LocalTime: true,
|
||||
Compress: true,
|
||||
FileMode: 0o644,
|
||||
Formatter: &nested.Formatter{
|
||||
TimestampFormat: "2006-01-02 15:04:05",
|
||||
HideKeys: true,
|
||||
NoColors: true,
|
||||
},
|
||||
Level: logrus.DebugLevel,
|
||||
Enabled: true,
|
||||
})
|
||||
|
||||
log.AddHook(hook)
|
||||
|
||||
log.Infoln("just info message")
|
||||
log.Debugln("first debug message")
|
||||
|
||||
hook.SetEnabled(false)
|
||||
log.Debugln("second debug message")
|
||||
```
|
||||
@@ -0,0 +1,10 @@
|
||||
module git.corp.kornet35.ru/gopkg/rotatehook
|
||||
|
||||
go 1.25.5
|
||||
|
||||
require (
|
||||
git.corp.kornet35.ru/gopkg/logrus v0.0.0-20260103190809-0c856ce1f510
|
||||
git.corp.kornet35.ru/gopkg/lumberjack/v2 v2.0.0-20260103183310-6bd4f9281683
|
||||
)
|
||||
|
||||
require golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
|
||||
@@ -0,0 +1,17 @@
|
||||
git.corp.kornet35.ru/gopkg/logrus v0.0.0-20260103190809-0c856ce1f510 h1:Ill+HjUQDH442NPfZfD5Nt5mC+MjomXJjV3/EPi3brU=
|
||||
git.corp.kornet35.ru/gopkg/logrus v0.0.0-20260103190809-0c856ce1f510/go.mod h1:RwD+Te62on5EpPYJD9BKz+Vp9kVfhA0qpHNkC9IFPgA=
|
||||
git.corp.kornet35.ru/gopkg/lumberjack/v2 v2.0.0-20260103183310-6bd4f9281683 h1:h6sq3Sc9dzF99Zh0kuucUTA+LwZJWILqONF2UWHF0qg=
|
||||
git.corp.kornet35.ru/gopkg/lumberjack/v2 v2.0.0-20260103183310-6bd4f9281683/go.mod h1:wAUcgTd7sG0LZRIH+gPL2J613C07xbfW2xTfUYR3plA=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
|
||||
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/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
@@ -0,0 +1,74 @@
|
||||
package rotatehook
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"git.corp.kornet35.ru/gopkg/logrus"
|
||||
"git.corp.kornet35.ru/gopkg/lumberjack/v2"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Filename string
|
||||
MaxSize int
|
||||
MaxAge int
|
||||
MaxBackups int
|
||||
LocalTime bool
|
||||
Compress bool
|
||||
// FileMode os.FileMode
|
||||
Formatter logrus.Formatter
|
||||
Level logrus.Level
|
||||
Enabled bool
|
||||
}
|
||||
|
||||
type RotateHook struct {
|
||||
logger *lumberjack.Logger
|
||||
mu sync.RWMutex
|
||||
cfg *Config
|
||||
}
|
||||
|
||||
func NewRotateHook(cfg *Config) *RotateHook {
|
||||
h := &RotateHook{
|
||||
cfg: cfg,
|
||||
logger: &lumberjack.Logger{
|
||||
Filename: cfg.Filename,
|
||||
MaxSize: cfg.MaxSize,
|
||||
MaxAge: cfg.MaxAge,
|
||||
MaxBackups: cfg.MaxBackups,
|
||||
LocalTime: cfg.LocalTime,
|
||||
Compress: cfg.Compress,
|
||||
// FileMode: cfg.FileMode,
|
||||
},
|
||||
}
|
||||
|
||||
return h
|
||||
}
|
||||
|
||||
func (h *RotateHook) Levels() []logrus.Level {
|
||||
h.mu.RLock()
|
||||
defer h.mu.RUnlock()
|
||||
return logrus.AllLevels[:h.cfg.Level+1]
|
||||
}
|
||||
|
||||
func (h *RotateHook) Fire(entry *logrus.Entry) (err error) {
|
||||
b, err := h.cfg.Formatter.Format(entry)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if h.Enabled() {
|
||||
h.logger.Write(b)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (h *RotateHook) Enabled() bool {
|
||||
h.mu.RLock()
|
||||
defer h.mu.RUnlock()
|
||||
return h.cfg.Enabled
|
||||
}
|
||||
|
||||
func (h *RotateHook) SetEnabled(enabled bool) {
|
||||
h.mu.Lock()
|
||||
defer h.mu.Unlock()
|
||||
h.cfg.Enabled = enabled
|
||||
}
|
||||
Reference in New Issue
Block a user