mirror of
https://github.com/Fishwaldo/go-logadapter.git
synced 2025-03-15 19:31:25 +00:00
Add SetPrefix/GetPrefix to Logger (#1)
* Add SetPrefix/GetPrefix to Logger * GoReadme and codecov actions update
This commit is contained in:
parent
538f0495a3
commit
d5e8061910
4 changed files with 61 additions and 5 deletions
30
.github/workflows/build.yml
vendored
30
.github/workflows/build.yml
vendored
|
@ -1,4 +1,12 @@
|
|||
on: [ push, pull_request ]
|
||||
on:
|
||||
push:
|
||||
branches: [main]
|
||||
pull_request:
|
||||
branches: [main]
|
||||
permissions:
|
||||
# Goreadme needs permissions to update pull requests comments and change contents.
|
||||
pull-requests: write
|
||||
contents: write
|
||||
name: Build
|
||||
jobs:
|
||||
test:
|
||||
|
@ -16,6 +24,20 @@ jobs:
|
|||
- name: Checkout code
|
||||
uses: actions/checkout@v1
|
||||
- name: Run tests
|
||||
run: go test -v -race -covermode=atomic -coverprofile=coverage
|
||||
- name: Upload coverage to Codecov
|
||||
run: bash <(curl -s https://codecov.io/bash)
|
||||
run: go test -v -race -covermode=atomic -coverprofile=coverage.out
|
||||
- name: CodeCov
|
||||
uses: codecov/codecov-action@v2
|
||||
goreadme:
|
||||
runs-on: ubuntu-latest
|
||||
needs: test
|
||||
steps:
|
||||
- name: Check out repository
|
||||
uses: actions/checkout@v2
|
||||
- name: Update readme according to Go doc
|
||||
uses: posener/goreadme@v1
|
||||
with:
|
||||
badge-codecov: 'true'
|
||||
badge-godoc: 'true'
|
||||
badge-goreadme: 'true'
|
||||
# Optional: Token allows goreadme to comment the PR with diff preview.
|
||||
github-token: '${{ secrets.GITHUB_TOKEN }}'
|
||||
|
|
17
log.go
17
log.go
|
@ -6,6 +6,7 @@ import (
|
|||
"log"
|
||||
"sync"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/Fishwaldo/go-logadapter/utils"
|
||||
)
|
||||
|
@ -20,12 +21,14 @@ type Logger interface {
|
|||
Panic(message string, params ...interface{})
|
||||
New(name string) (l Logger)
|
||||
With(key string, value interface{}) (l Logger)
|
||||
SetPrefix(name string)
|
||||
GetPrefix() (string)
|
||||
Sync()
|
||||
}
|
||||
|
||||
//DefaultLogger uses Golang Standard Logging Libary
|
||||
func DefaultLogger() (l *StdLogger) {
|
||||
stdlogger := log.New(os.Stderr, "sched - ", log.LstdFlags)
|
||||
stdlogger := log.New(os.Stderr, "log - ", log.LstdFlags)
|
||||
stdlog := &StdLogger{Log: *stdlogger, keys: make(map[string]interface{})}
|
||||
return stdlog
|
||||
}
|
||||
|
@ -119,3 +122,15 @@ func (l *StdLogger) SetLevel(level Log_Level) {
|
|||
func (l *StdLogger) GetLevel() (level Log_Level) {
|
||||
return l.level
|
||||
}
|
||||
|
||||
func (l *StdLogger) SetPrefix(name string) {
|
||||
l.Log.SetPrefix(fmt.Sprintf("%s - ", name))
|
||||
}
|
||||
|
||||
func (l *StdLogger) GetPrefix() (string) {
|
||||
temp := strings.Fields(l.Log.Prefix())
|
||||
if len(temp) > 0 {
|
||||
return temp[0]
|
||||
}
|
||||
return ""
|
||||
}
|
|
@ -1,6 +1,8 @@
|
|||
package logruslog
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/Fishwaldo/go-logadapter"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
@ -43,7 +45,17 @@ func (l *LruLogger) New(name string) logadapter.Logger {
|
|||
nl := &LruLogger{Lru: logrus.NewEntry(l.Lru.Logger)}
|
||||
return nl
|
||||
}
|
||||
func (l *LruLogger) SetPrefix(name string) {
|
||||
l.Lru = l.Lru.WithField("Prefix", name)
|
||||
}
|
||||
|
||||
func (l *LruLogger) GetPrefix() (string) {
|
||||
prefix, ok := l.Lru.Data["Prefix"]
|
||||
if ok {
|
||||
return fmt.Sprint(prefix)
|
||||
}
|
||||
return ""
|
||||
}
|
||||
//LogrusDefaultLogger Return Logger based on logrus with new instance
|
||||
func LogrusDefaultLogger() logadapter.Logger {
|
||||
// TODO control verbosity
|
||||
|
|
|
@ -47,6 +47,13 @@ func (l *ZapLogger) Sync() {
|
|||
|
||||
}
|
||||
|
||||
func (l *ZapLogger) SetPrefix(name string) {
|
||||
// XXX TODO
|
||||
}
|
||||
func (l ZapLogger) GetPrefix() (string) {
|
||||
return ""
|
||||
}
|
||||
|
||||
//DefaultLogger Return Default Sched Logger based on Zap's sugared logger
|
||||
func NewZapLogger() *ZapLogger {
|
||||
// TODO control verbosity
|
||||
|
|
Loading…
Add table
Reference in a new issue