diff options
author | zeripath <art27@cantab.net> | 2020-07-06 01:07:07 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-07-05 20:07:07 -0400 |
commit | c5b08f6d5a73e6ba84da84e804d05a8dd3d651be (patch) | |
tree | 7e0f72b9c62b8764d000614714c91a01ebbee223 /modules/setting/log.go | |
parent | 38fb087d1983ca8320fb1a8c90150ae7956b358d (diff) | |
download | gitea-c5b08f6d5a73e6ba84da84e804d05a8dd3d651be.tar.gz gitea-c5b08f6d5a73e6ba84da84e804d05a8dd3d651be.zip |
Pause, Resume, Release&Reopen, Add and Remove Logging from command line (#11777)
* Make LogDescriptions race safe
* Add manager commands for pausing, resuming, adding and removing loggers
Signed-off-by: Andrew Thornton <art27@cantab.net>
* Placate lint
* Ensure that file logger is run!
* Add support for smtp and conn
Signed-off-by: Andrew Thornton <art27@cantab.net>
* Add release-and-reopen
Signed-off-by: Andrew Thornton <art27@cantab.net>
Co-authored-by: techknowlogick <techknowlogick@gitea.io>
Co-authored-by: Lauris BH <lauris@nix.lv>
Diffstat (limited to 'modules/setting/log.go')
-rw-r--r-- | modules/setting/log.go | 68 |
1 files changed, 66 insertions, 2 deletions
diff --git a/modules/setting/log.go b/modules/setting/log.go index 5ffb2479dd..35bf021ac2 100644 --- a/modules/setting/log.go +++ b/modules/setting/log.go @@ -12,6 +12,7 @@ import ( "path" "path/filepath" "strings" + "sync" "code.gitea.io/gitea/modules/log" @@ -20,6 +21,69 @@ import ( var filenameSuffix = "" +var descriptionLock = sync.RWMutex{} +var logDescriptions = make(map[string]*LogDescription) + +// GetLogDescriptions returns a race safe set of descriptions +func GetLogDescriptions() map[string]*LogDescription { + descriptionLock.RLock() + defer descriptionLock.RUnlock() + descs := make(map[string]*LogDescription, len(logDescriptions)) + for k, v := range logDescriptions { + subLogDescriptions := make([]SubLogDescription, len(v.SubLogDescriptions)) + for i, s := range v.SubLogDescriptions { + subLogDescriptions[i] = s + } + descs[k] = &LogDescription{ + Name: v.Name, + SubLogDescriptions: subLogDescriptions, + } + } + return descs +} + +// AddLogDescription adds a set of descriptions to the complete description +func AddLogDescription(key string, description *LogDescription) { + descriptionLock.Lock() + defer descriptionLock.Unlock() + logDescriptions[key] = description +} + +// AddSubLogDescription adds a sub log description +func AddSubLogDescription(key string, subLogDescription SubLogDescription) bool { + descriptionLock.Lock() + defer descriptionLock.Unlock() + desc, ok := logDescriptions[key] + if !ok { + return false + } + for i, sub := range desc.SubLogDescriptions { + if sub.Name == subLogDescription.Name { + desc.SubLogDescriptions[i] = subLogDescription + return true + } + } + desc.SubLogDescriptions = append(desc.SubLogDescriptions, subLogDescription) + return true +} + +// RemoveSubLogDescription removes a sub log description +func RemoveSubLogDescription(key string, name string) bool { + descriptionLock.Lock() + defer descriptionLock.Unlock() + desc, ok := logDescriptions[key] + if !ok { + return false + } + for i, sub := range desc.SubLogDescriptions { + if sub.Name == name { + desc.SubLogDescriptions = append(desc.SubLogDescriptions[:i], desc.SubLogDescriptions[i+1:]...) + return true + } + } + return false +} + type defaultLogOptions struct { levelName string // LogLevel flags string @@ -185,7 +249,7 @@ func generateNamedLogger(key string, options defaultLogOptions) *LogDescription log.Info("%s Log: %s(%s:%s)", strings.Title(key), strings.Title(name), provider, levelName) } - LogDescriptions[key] = &description + AddLogDescription(key, &description) return &description } @@ -279,7 +343,7 @@ func newLogService() { log.Info("Gitea Log Mode: %s(%s:%s)", strings.Title(name), strings.Title(provider), levelName) } - LogDescriptions[log.DEFAULT] = &description + AddLogDescription(log.DEFAULT, &description) // Finally redirect the default golog to here golog.SetFlags(0) |