Ejemplo n.º 1
0
func getLogFormat(format string) log15.Format {
	switch format {
	case "console":
		if term.IsTty(os.Stdout.Fd()) {
			return log15.TerminalFormat()
		}
		return log15.LogfmtFormat()
	case "text":
		return log15.LogfmtFormat()
	case "json":
		return log15.JsonFormat()
	default:
		return log15.LogfmtFormat()
	}
}
Ejemplo n.º 2
0
func newLogger() log15.Logger {
	l := log15.New()
	h := log15.StreamHandler(os.Stdout, log15.LogfmtFormat())
	//h = log15.CallerStackHandler("%+n", h)
	l.SetHandler(log15.LazyHandler(h))
	return l
}
Ejemplo n.º 3
0
// create a FileLogWriter returning as LoggerInterface.
func NewFileWriter() *FileLogWriter {
	w := &FileLogWriter{
		Filename: "",
		Format:   log15.LogfmtFormat(),
		Maxlines: 1000000,
		Maxsize:  1 << 28, //256 MB
		Daily:    true,
		Maxdays:  7,
		Rotate:   true,
	}
	// use MuxWriter instead direct use os.File for lock write when rotate
	w.mw = new(MuxWriter)
	return w
}
Ejemplo n.º 4
0
// getMsg returns message which should be sent to Slack
func (h *Handler) getMsg(r *log15.Record) *message {
	msg := &message{
		Envelope: h.Envelope,
	}

	// Choose message color depending on log level
	// (this is imitating colors from log15.TerminalFormat)
	color := "#32C8C8" // blue
	switch r.Lvl {
	case log15.LvlInfo:
		color = "good" // green
	case log15.LvlWarn:
		color = "warning" // yellow
	case log15.LvlError:
		color = "danger" // red
	case log15.LvlCrit:
		color = "#C832C8" // purple
	}
	msg.Attachments[0].Color = color

	if h.Formatter != nil {
		txt := string(h.Formatter.Format(r))
		msg.Attachments[0].Text = txt
		msg.Attachments[0].Fallback = txt
	} else {
		ctx := newCtxReader(r.Ctx)
		fields := make([]Field, 0, len(r.Ctx)/2)

		for ctx.Next() {
			v := ctx.Value()
			// See if value is a Field; if not, fill with defaults
			f, ok := v.(Field)
			if !ok {
				f.Value = fmt.Sprintf("%+v", v)
				f.Short = true
			}
			if f.Title == "" {
				f.Title = ctx.Key()
			}
			fields = append(fields, f)
		}

		msg.Attachments[0].Text = r.Msg
		msg.Attachments[0].Fallback = string(log15.LogfmtFormat().Format(r))
		msg.Attachments[0].Fields = fields
	}

	return msg
}
Ejemplo n.º 5
0
func newLogger(c *Context) (log15.Logger, error) {
	lvl := c.String(FlagLogLevel)
	l := log15.New()
	log.Println(fmt.Sprintf("Using log level %s", lvl))
	v, err := log15.LvlFromString(lvl)
	if err != nil {
		return l, err
	}
	h := log15.LvlFilterHandler(v, log15.StreamHandler(os.Stdout, log15.LogfmtFormat()))
	if lvl == "debug" {
		h = log15.CallerFileHandler(h)
	}
	l.SetHandler(log15.LazyHandler(h))
	return l, err
}
Ejemplo n.º 6
0
func NewSyslog(sec *ini.Section, format log15.Format) *SysLogHandler {
	handler := &SysLogHandler{
		Format: log15.LogfmtFormat(),
	}

	handler.Format = format
	handler.Network = sec.Key("network").MustString("")
	handler.Address = sec.Key("address").MustString("")
	handler.Facility = sec.Key("facility").MustString("local7")
	handler.Tag = sec.Key("tag").MustString("")

	if err := handler.Init(); err != nil {
		Root.Error("Failed to init syslog log handler", "error", err)
		os.Exit(1)
	}

	return handler
}
Ejemplo n.º 7
0
// DebugContext returns a new Context with an additional handler with a more
// verbose filter (using the Debug level) and a Buffer in which all logging
// statements will be (also) written to.
func (context *Context) DebugContext(logCtx ...interface{}) *Context {
	var buffer bytes.Buffer
	childContext := &Context{
		Config:    context.Config,
		Log:       context.Log.New(logCtx...),
		logBuffer: &buffer,
		handler: log15.MultiHandler(
			log15.StreamHandler(&buffer, log15.LogfmtFormat()),
			context.handler,
		),
		memoryCollector: NewMemoryEventCollector(),
		EventFactory:    context.EventFactory,
	}
	childContext.EventCollector = NewMultiEventCollector(
		childContext.memoryCollector,
		context.EventCollector,
	)
	childContext.EventFactory.Register(childContext.memoryCollector)
	childContext.Log.SetHandler(childContext.handler)
	return childContext
}
Ejemplo n.º 8
0
func newLogger() log15.Logger {
	l := log15.New()
	h := log15.StreamHandler(os.Stdout, log15.LogfmtFormat())
	l.SetHandler(log15.LazyHandler(h))
	return l
}
Ejemplo n.º 9
0
// NewContext creates a new Context from the specified Config. This also
// creates a Logger.
func NewContext(config *Config) (*Context, error) {
	var context = Context{
		Config: *config,
	}

	// Logging
	context.Log = log15.New()
	if context.Config.Logging.File == "/dev/null" {
		context.handler = log15.DiscardHandler()
	} else if context.Config.Logging.File == "stderr" {
		context.handler = log15.StderrHandler
	} else {
		handler, err := log15.FileHandler(
			context.Config.Logging.File,
			log15.LogfmtFormat(),
		)
		if err != nil {
			return nil, err
		}
		context.handler = handler
	}
	level, err := log15.LvlFromString(context.Config.Logging.Level)
	if err != nil {
		return nil, err
	}
	context.handler = log15.LvlFilterHandler(level, context.handler)
	context.Log.SetHandler(context.handler)

	// Tracing
	if context.Config.Tracing.Enabled {
		s, err := os.Stat(context.Config.Tracing.File)
		if os.IsNotExist(err) || s.Size() == 0 {
			context.tracingFd, err = os.Create(context.Config.Tracing.File)
			if err != nil {
				return nil, err
			}
			context.EventCollector, err = NewWriterEventCollector(
				context.tracingFd,
				false,
			)
			if err != nil {
				return nil, err
			}
		} else {
			context.tracingFd, err = os.OpenFile(
				context.Config.Tracing.File,
				os.O_WRONLY|os.O_APPEND,
				0664,
			)
			if err != nil {
				return nil, err
			}
			context.EventCollector, err = NewWriterEventCollector(
				context.tracingFd,
				true,
			)
			if err != nil {
				return nil, err
			}
		}
	} else {
		context.EventCollector = &NullEventCollector{}
	}
	hostname, err := os.Hostname()
	if err != nil {
		hostname = "main"
	}
	context.EventFactory = NewEventFactory(hostname, "main")
	context.EventFactory.Register(context.EventCollector)

	return &context, nil
}
Ejemplo n.º 10
0
func nullLogger() log15.Logger {
	l := log15.New()
	h := log15.StreamHandler(ioutil.Discard, log15.LogfmtFormat())
	l.SetHandler(h)
	return l
}
Ejemplo n.º 11
0
func NewSyslog() *SysLogHandler {
	return &SysLogHandler{
		Format: log15.LogfmtFormat(),
	}
}