mirror of https://github.com/gogs/gogs.git
models: add config options for XORM logger (#3183)
Added new config section '[log.xorm]'.pull/4343/head
parent
66c1e6b0e8
commit
8a3f4fc616
10
conf/app.ini
10
conf/app.ini
|
@ -348,6 +348,16 @@ LEVEL =
|
|||
; Webhook URL
|
||||
URL =
|
||||
|
||||
[log.xorm]
|
||||
; Enable file rotation
|
||||
ROTATE = true
|
||||
; Rotate every day
|
||||
ROTATE_DAILY = true
|
||||
; Rotate once file size excesses x MB
|
||||
MAX_SIZE = 100
|
||||
; Maximum days to keep logger files
|
||||
MAX_DAYS = 3
|
||||
|
||||
[cron]
|
||||
; Enable running cron tasks periodically.
|
||||
ENABLED = true
|
||||
|
|
2
gogs.go
2
gogs.go
|
@ -16,7 +16,7 @@ import (
|
|||
"github.com/gogits/gogs/modules/setting"
|
||||
)
|
||||
|
||||
const APP_VER = "0.10.26.0323"
|
||||
const APP_VER = "0.10.27.0323"
|
||||
|
||||
func init() {
|
||||
setting.AppVer = APP_VER
|
||||
|
|
|
@ -21,6 +21,7 @@ import (
|
|||
"github.com/go-xorm/core"
|
||||
"github.com/go-xorm/xorm"
|
||||
_ "github.com/lib/pq"
|
||||
log "gopkg.in/clog.v1"
|
||||
|
||||
"github.com/gogits/gogs/models/migrations"
|
||||
"github.com/gogits/gogs/modules/setting"
|
||||
|
@ -192,18 +193,23 @@ func SetEngine() (err error) {
|
|||
|
||||
// WARNING: for serv command, MUST remove the output to os.stdout,
|
||||
// so use log file to instead print to stdout.
|
||||
logPath := path.Join(setting.LogRootPath, "xorm.log")
|
||||
os.MkdirAll(path.Dir(logPath), os.ModePerm)
|
||||
|
||||
f, err := os.Create(logPath)
|
||||
sec := setting.Cfg.Section("log.xorm")
|
||||
fmt.Println(sec.Key("ROTATE_DAILY").MustBool(true))
|
||||
logger, err := log.NewFileWriter(path.Join(setting.LogRootPath, "xorm.log"),
|
||||
log.FileRotationConfig{
|
||||
Rotate: sec.Key("ROTATE").MustBool(true),
|
||||
Daily: sec.Key("ROTATE_DAILY").MustBool(true),
|
||||
MaxSize: sec.Key("MAX_SIZE").MustInt64(100) * 1024 * 1024,
|
||||
MaxDays: sec.Key("MAX_DAYS").MustInt64(3),
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("Fail to create xorm.log: %v", err)
|
||||
return fmt.Errorf("Fail to create 'xorm.log': %v", err)
|
||||
}
|
||||
|
||||
if setting.ProdMode {
|
||||
x.SetLogger(xorm.NewSimpleLogger3(f, xorm.DEFAULT_LOG_PREFIX, xorm.DEFAULT_LOG_FLAG, core.LOG_WARNING))
|
||||
x.SetLogger(xorm.NewSimpleLogger3(logger, xorm.DEFAULT_LOG_PREFIX, xorm.DEFAULT_LOG_FLAG, core.LOG_WARNING))
|
||||
} else {
|
||||
x.SetLogger(xorm.NewSimpleLogger(f))
|
||||
x.SetLogger(xorm.NewSimpleLogger(logger))
|
||||
}
|
||||
x.ShowSQL(true)
|
||||
return nil
|
||||
|
|
File diff suppressed because one or more lines are too long
|
@ -1 +1 @@
|
|||
0.10.26.0323
|
||||
0.10.27.0323
|
|
@ -28,7 +28,7 @@ Please apply `-u` flag to update in the future.
|
|||
|
||||
## Getting Started
|
||||
|
||||
Clog currently has two builtin logger adapters: `console`, `file` and `slack`.
|
||||
Clog currently has three builtin logger adapters: `console`, `file` and `slack`.
|
||||
|
||||
It is extremely easy to create one with all default settings. Generally, you would want to create new logger inside `init` or `main` function.
|
||||
|
||||
|
@ -138,6 +138,8 @@ Slack logger is also supported in a simple way:
|
|||
...
|
||||
```
|
||||
|
||||
This logger also works for [Discord Slack](https://discordapp.com/developers/docs/resources/webhook#execute-slackcompatible-webhook) endpoint.
|
||||
|
||||
## Credits
|
||||
|
||||
- Avatar is a modified version based on [egonelbre/gophers' scientist](https://github.com/egonelbre/gophers/blob/master/vector/science/scientist.svg).
|
||||
|
|
|
@ -24,7 +24,7 @@ import (
|
|||
)
|
||||
|
||||
const (
|
||||
_VERSION = "1.0.2"
|
||||
_VERSION = "1.1.0"
|
||||
)
|
||||
|
||||
// Version returns current version of the package.
|
||||
|
|
|
@ -17,6 +17,7 @@ package clog
|
|||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
|
@ -58,6 +59,9 @@ type FileConfig struct {
|
|||
}
|
||||
|
||||
type file struct {
|
||||
// Indicates whether object is been used in standalone mode.
|
||||
standalone bool
|
||||
|
||||
*log.Logger
|
||||
Adapter
|
||||
|
||||
|
@ -77,6 +81,21 @@ func newFile() Logger {
|
|||
}
|
||||
}
|
||||
|
||||
// NewFileWriter returns an io.Writer for synchronized file logger in standalone mode.
|
||||
func NewFileWriter(filename string, cfg FileRotationConfig) (io.Writer, error) {
|
||||
f := &file{
|
||||
standalone: true,
|
||||
}
|
||||
if err := f.Init(FileConfig{
|
||||
Filename: filename,
|
||||
FileRotationConfig: cfg,
|
||||
}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return f, nil
|
||||
}
|
||||
|
||||
func (f *file) Level() LEVEL { return f.level }
|
||||
|
||||
var newLineBytes = []byte("\n")
|
||||
|
@ -196,7 +215,9 @@ func (f *file) Init(v interface{}) (err error) {
|
|||
f.initRotate()
|
||||
}
|
||||
|
||||
f.msgChan = make(chan *Message, cfg.BufferSize)
|
||||
if !f.standalone {
|
||||
f.msgChan = make(chan *Message, cfg.BufferSize)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -205,11 +226,15 @@ func (f *file) ExchangeChans(errorChan chan<- error) chan *Message {
|
|||
return f.msgChan
|
||||
}
|
||||
|
||||
func (f *file) write(msg *Message) {
|
||||
func (f *file) write(msg *Message) int {
|
||||
f.Logger.Print(msg.Body)
|
||||
|
||||
bytesWrote := len(msg.Body)
|
||||
if !f.standalone {
|
||||
bytesWrote += LOG_PREFIX_LENGTH
|
||||
}
|
||||
if f.rotate.Rotate {
|
||||
f.currentSize += int64(LOG_PREFIX_LENGTH + len(msg.Body))
|
||||
f.currentSize += int64(bytesWrote)
|
||||
f.currentLines++ // TODO: should I care if log message itself contains new lines?
|
||||
|
||||
var (
|
||||
|
@ -243,6 +268,16 @@ func (f *file) write(msg *Message) {
|
|||
f.currentLines = 0
|
||||
}
|
||||
}
|
||||
return bytesWrote
|
||||
}
|
||||
|
||||
var _ io.Writer = new(file)
|
||||
|
||||
// Write implements method of io.Writer interface.
|
||||
func (f *file) Write(p []byte) (int, error) {
|
||||
return f.write(&Message{
|
||||
Body: string(p),
|
||||
}), nil
|
||||
}
|
||||
|
||||
func (f *file) Start() {
|
||||
|
|
|
@ -92,8 +92,12 @@ func (s *slack) ExchangeChans(errorChan chan<- error) chan *Message {
|
|||
return s.msgChan
|
||||
}
|
||||
|
||||
func buildSlackAttachment(msg *Message) string {
|
||||
return fmt.Sprintf(_SLACK_ATTACHMENT, msg.Body, slackColors[msg.Level])
|
||||
}
|
||||
|
||||
func (s *slack) write(msg *Message) {
|
||||
attachment := fmt.Sprintf(_SLACK_ATTACHMENT, msg.Body, slackColors[msg.Level])
|
||||
attachment := buildSlackAttachment(msg)
|
||||
resp, err := http.Post(s.url, "application/json", bytes.NewReader([]byte(attachment)))
|
||||
if err != nil {
|
||||
s.errorChan <- fmt.Errorf("slack: %v", err)
|
||||
|
|
|
@ -525,10 +525,10 @@
|
|||
"revisionTime": "2015-09-24T05:17:56Z"
|
||||
},
|
||||
{
|
||||
"checksumSHA1": "TlOZMrb/wY8vsDIGkXlmxsbDLP0=",
|
||||
"checksumSHA1": "ZJBrUSDBKgkXID1MVRkXSTlmOh4=",
|
||||
"path": "gopkg.in/clog.v1",
|
||||
"revision": "bf4bf4a49c663cd0963f8775a4b60d30a75098d1",
|
||||
"revisionTime": "2017-02-17T23:04:09Z"
|
||||
"revision": "ff5a366d133e02b3d411dbe3854ebd912a434c7f",
|
||||
"revisionTime": "2017-03-23T22:33:02Z"
|
||||
},
|
||||
{
|
||||
"checksumSHA1": "LIu3jihd3edOyIsJJK3V6vx2UZg=",
|
||||
|
|
Loading…
Reference in New Issue