-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
127 additions
and
7 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,54 @@ | ||
package siserlogger | ||
|
||
import ( | ||
"path/filepath" | ||
"sync" | ||
"time" | ||
|
||
"github.com/kjk/common/filerotate" | ||
"github.com/kjk/common/siser" | ||
) | ||
|
||
type Logger struct { | ||
siser *siser.Writer | ||
file *filerotate.File | ||
name string | ||
RecName string | ||
mu sync.Mutex | ||
dir string | ||
} | ||
|
||
func NewDaily(dir string, name string, didRotateFn func(path string)) (*Logger, error) { | ||
absDir, err := filepath.Abs(dir) | ||
if err != nil { | ||
return nil, err | ||
} | ||
res := &Logger{ | ||
dir: absDir, | ||
name: name, | ||
RecName: name, | ||
} | ||
|
||
didRotateInternal := func(path string, didRotate bool) { | ||
if didRotate && didRotateFn != nil { | ||
didRotateFn(path) | ||
} | ||
} | ||
|
||
res.file, err = filerotate.NewDaily(absDir, "slog-"+name+"-", didRotateInternal) | ||
if err != nil { | ||
return nil, err | ||
} | ||
res.siser = siser.NewWriter(res.file) | ||
return res, nil | ||
} | ||
|
||
func (l *Logger) Write(d []byte) error { | ||
l.mu.Lock() | ||
defer l.mu.Unlock() | ||
if l.siser == nil { | ||
return nil | ||
} | ||
_, err := l.siser.Write(d, time.Now(), l.RecName) | ||
return err | ||
} |