예제 #1
0
파일: factories.go 프로젝트: iserko/empire
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
}
예제 #2
0
func init() {
	Log = log.New()
	var handler log.Handler
	env := config.Viper.GetString("env")
	lvl, err := log.LvlFromString(config.Viper.GetString("LogLevel"))
	if err != nil {
		panic("Could not read configuration for LogLevel, check the 'config-" + env +
			".json' file: " + err.Error())
	}
	if env == "dev" || env == "qa" {
		handler = logext.FatalHandler(log.LvlFilterHandler(lvl, log.CallerFileHandler(log.StdoutHandler)))
	} else {
		handler = logext.FatalHandler(log.LvlFilterHandler(lvl, log.CallerFileHandler(log.StreamHandler(os.Stdout, log.JsonFormat()))))
	}
	Log.SetHandler(handler)
}
예제 #3
0
파일: context.go 프로젝트: lhchavez/quark
// 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
}