cmd, internal, log: write logs into 1MB file chunks

This commit is contained in:
Kurkó Mihály 2018-03-09 14:31:10 +02:00
parent ccc0debb63
commit c5549810a5
4 changed files with 39 additions and 4 deletions

View file

@ -185,7 +185,7 @@ func init() {
app.Before = func(ctx *cli.Context) error {
runtime.GOMAXPROCS(runtime.NumCPU())
if err := debug.Setup(ctx); err != nil {
if err := debug.Setup(ctx, ctx.GlobalBool(utils.DashboardEnabledFlag.Name), utils.DataDirFlag.Value.Value); err != nil {
return err
}
// Start system runtime metrics collection

View file

@ -363,7 +363,7 @@ DEPRECATED: use 'swarm db clean'.
app.Flags = append(app.Flags, swarmmetrics.Flags...)
app.Before = func(ctx *cli.Context) error {
runtime.GOMAXPROCS(runtime.NumCPU())
if err := debug.Setup(ctx); err != nil {
if err := debug.Setup(ctx, ctx.GlobalBool(utils.DashboardEnabledFlag.Name), utils.DataDirFlag.Value.Value); err != nil {
return err
}
swarmmetrics.Setup(ctx)

View file

@ -108,13 +108,17 @@ func init() {
// Setup initializes profiling and logging based on the CLI flags.
// It should be called as early as possible in the program.
func Setup(ctx *cli.Context) error {
func Setup(ctx *cli.Context, dashboard bool, path string) error {
// logging
log.PrintOrigins(ctx.GlobalBool(debugFlag.Name))
glogger.Verbosity(log.Lvl(ctx.GlobalInt(verbosityFlag.Name)))
glogger.Vmodule(ctx.GlobalString(vmoduleFlag.Name))
glogger.BacktraceAt(ctx.GlobalString(backtraceAtFlag.Name))
log.Root().SetHandler(glogger)
h := log.Handler(glogger)
if dashboard {
h = log.MultiHandler(h, log.DashboardHandler(path+"/dashboard/logs"))
}
log.Root().SetHandler(h)
// profiling, tracing
runtime.MemProfileRate = ctx.GlobalInt(memprofilerateFlag.Name)

View file

@ -9,6 +9,7 @@ import (
"sync"
"github.com/go-stack/stack"
"strings"
)
// Handler defines where and how log records are written.
@ -70,6 +71,36 @@ func FileHandler(path string, fmtr Format) (Handler, error) {
return closingHandler{f, StreamHandler(f, fmtr)}, nil
}
// DashboardHandler returns a handler which writes log records to file chunks
// at the given path. When a file's size reaches the 1MB, the handler creates
// a new file named with the timestamp of the first log record it will contain.
func DashboardHandler(path string) Handler {
if _, err := os.Stat(path); os.IsNotExist(err) {
if err = os.MkdirAll(path, 0755); err != nil {
// TODO (kurkomisi): handle error?
return DiscardHandler()
}
}
var size uint
maxSize := uint(1048576)
var h Handler
formatter := JsonFormat()
return FuncHandler(func(r *Record) error {
var err error
if h == nil || size > maxSize {
if h, err = FileHandler(fmt.Sprintf("%s/%s.log", path,
strings.Replace(r.Time.Format("060102150405.00"), ".", "", 1)), formatter); err != nil {
return err
}
size = 0
}
size += uint(len(formatter.Format(r)))
return h.Log(r)
})
}
// NetHandler opens a socket to the given address and writes records
// over the connection.
func NetHandler(network, addr string, fmtr Format) (Handler, error) {