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
|
; Webhook URL
|
||||||
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]
|
[cron]
|
||||||
; Enable running cron tasks periodically.
|
; Enable running cron tasks periodically.
|
||||||
ENABLED = true
|
ENABLED = true
|
||||||
|
|
2
gogs.go
2
gogs.go
|
@ -16,7 +16,7 @@ import (
|
||||||
"github.com/gogits/gogs/modules/setting"
|
"github.com/gogits/gogs/modules/setting"
|
||||||
)
|
)
|
||||||
|
|
||||||
const APP_VER = "0.10.26.0323"
|
const APP_VER = "0.10.27.0323"
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
setting.AppVer = APP_VER
|
setting.AppVer = APP_VER
|
||||||
|
|
|
@ -21,6 +21,7 @@ import (
|
||||||
"github.com/go-xorm/core"
|
"github.com/go-xorm/core"
|
||||||
"github.com/go-xorm/xorm"
|
"github.com/go-xorm/xorm"
|
||||||
_ "github.com/lib/pq"
|
_ "github.com/lib/pq"
|
||||||
|
log "gopkg.in/clog.v1"
|
||||||
|
|
||||||
"github.com/gogits/gogs/models/migrations"
|
"github.com/gogits/gogs/models/migrations"
|
||||||
"github.com/gogits/gogs/modules/setting"
|
"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,
|
// WARNING: for serv command, MUST remove the output to os.stdout,
|
||||||
// so use log file to instead print to stdout.
|
// so use log file to instead print to stdout.
|
||||||
logPath := path.Join(setting.LogRootPath, "xorm.log")
|
sec := setting.Cfg.Section("log.xorm")
|
||||||
os.MkdirAll(path.Dir(logPath), os.ModePerm)
|
fmt.Println(sec.Key("ROTATE_DAILY").MustBool(true))
|
||||||
|
logger, err := log.NewFileWriter(path.Join(setting.LogRootPath, "xorm.log"),
|
||||||
f, err := os.Create(logPath)
|
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 {
|
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 {
|
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 {
|
} else {
|
||||||
x.SetLogger(xorm.NewSimpleLogger(f))
|
x.SetLogger(xorm.NewSimpleLogger(logger))
|
||||||
}
|
}
|
||||||
x.ShowSQL(true)
|
x.ShowSQL(true)
|
||||||
return nil
|
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
|
## 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.
|
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
|
## Credits
|
||||||
|
|
||||||
- Avatar is a modified version based on [egonelbre/gophers' scientist](https://github.com/egonelbre/gophers/blob/master/vector/science/scientist.svg).
|
- 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 (
|
const (
|
||||||
_VERSION = "1.0.2"
|
_VERSION = "1.1.0"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Version returns current version of the package.
|
// Version returns current version of the package.
|
||||||
|
|
|
@ -17,6 +17,7 @@ package clog
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
@ -58,6 +59,9 @@ type FileConfig struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type file struct {
|
type file struct {
|
||||||
|
// Indicates whether object is been used in standalone mode.
|
||||||
|
standalone bool
|
||||||
|
|
||||||
*log.Logger
|
*log.Logger
|
||||||
Adapter
|
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 }
|
func (f *file) Level() LEVEL { return f.level }
|
||||||
|
|
||||||
var newLineBytes = []byte("\n")
|
var newLineBytes = []byte("\n")
|
||||||
|
@ -196,7 +215,9 @@ func (f *file) Init(v interface{}) (err error) {
|
||||||
f.initRotate()
|
f.initRotate()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !f.standalone {
|
||||||
f.msgChan = make(chan *Message, cfg.BufferSize)
|
f.msgChan = make(chan *Message, cfg.BufferSize)
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -205,11 +226,15 @@ func (f *file) ExchangeChans(errorChan chan<- error) chan *Message {
|
||||||
return f.msgChan
|
return f.msgChan
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *file) write(msg *Message) {
|
func (f *file) write(msg *Message) int {
|
||||||
f.Logger.Print(msg.Body)
|
f.Logger.Print(msg.Body)
|
||||||
|
|
||||||
|
bytesWrote := len(msg.Body)
|
||||||
|
if !f.standalone {
|
||||||
|
bytesWrote += LOG_PREFIX_LENGTH
|
||||||
|
}
|
||||||
if f.rotate.Rotate {
|
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?
|
f.currentLines++ // TODO: should I care if log message itself contains new lines?
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -243,6 +268,16 @@ func (f *file) write(msg *Message) {
|
||||||
f.currentLines = 0
|
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() {
|
func (f *file) Start() {
|
||||||
|
|
|
@ -92,8 +92,12 @@ func (s *slack) ExchangeChans(errorChan chan<- error) chan *Message {
|
||||||
return s.msgChan
|
return s.msgChan
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func buildSlackAttachment(msg *Message) string {
|
||||||
|
return fmt.Sprintf(_SLACK_ATTACHMENT, msg.Body, slackColors[msg.Level])
|
||||||
|
}
|
||||||
|
|
||||||
func (s *slack) write(msg *Message) {
|
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)))
|
resp, err := http.Post(s.url, "application/json", bytes.NewReader([]byte(attachment)))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.errorChan <- fmt.Errorf("slack: %v", err)
|
s.errorChan <- fmt.Errorf("slack: %v", err)
|
||||||
|
|
|
@ -525,10 +525,10 @@
|
||||||
"revisionTime": "2015-09-24T05:17:56Z"
|
"revisionTime": "2015-09-24T05:17:56Z"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"checksumSHA1": "TlOZMrb/wY8vsDIGkXlmxsbDLP0=",
|
"checksumSHA1": "ZJBrUSDBKgkXID1MVRkXSTlmOh4=",
|
||||||
"path": "gopkg.in/clog.v1",
|
"path": "gopkg.in/clog.v1",
|
||||||
"revision": "bf4bf4a49c663cd0963f8775a4b60d30a75098d1",
|
"revision": "ff5a366d133e02b3d411dbe3854ebd912a434c7f",
|
||||||
"revisionTime": "2017-02-17T23:04:09Z"
|
"revisionTime": "2017-03-23T22:33:02Z"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"checksumSHA1": "LIu3jihd3edOyIsJJK3V6vx2UZg=",
|
"checksumSHA1": "LIu3jihd3edOyIsJJK3V6vx2UZg=",
|
||||||
|
|
Loading…
Reference in New Issue