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>
This commit is contained in:
parent
38fb087d19
commit
c5b08f6d5a
17 changed files with 924 additions and 17 deletions
|
@ -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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue