Beispiel #1
0
func init() {
	dlog.SetLogger(NewLogger(protolog.NewStandardLogger(protolog.NewFileFlusher(os.Stderr))))
}
Beispiel #2
0
// SetupLogging sets up logging.
func SetupLogging(appName string, env Env) error {
	var pushers []protolog.Pusher
	if !env.DisableStderrLog {
		pushers = append(
			pushers,
			protolog.NewStandardWritePusher(
				protolog.NewFileFlusher(
					os.Stderr,
				),
			),
		)
	}
	if env.LogDir != "" {
		pushers = append(
			pushers,
			protolog.NewStandardWritePusher(
				protolog.NewWriterFlusher(
					&lumberjack.Logger{
						Filename:   filepath.Join(env.LogDir, fmt.Sprintf("%s.log", appName)),
						MaxBackups: 3,
					},
				),
			),
		)
	}
	if env.SyslogNetwork != "" && env.SyslogAddress != "" {
		writer, err := syslog.Dial(
			env.SyslogNetwork,
			env.SyslogAddress,
			syslog.LOG_INFO,
			appName,
		)
		if err != nil {
			return err
		}
		pushers = append(
			pushers,
			protosyslog.NewPusher(
				writer,
				protosyslog.PusherOptions{},
			),
		)
	}
	if len(pushers) > 0 {
		protolog.SetLogger(
			protolog.NewStandardLogger(
				protolog.NewMultiPusher(
					pushers...,
				),
			),
		)
	} else {
		protolog.SetLogger(
			protolog.DiscardLogger,
		)
	}
	protolog.RedirectStdLogger()
	if env.LogLevel != "" {
		levelValue, ok := protolog.Level_value[fmt.Sprintf("LEVEL_%s", strings.ToUpper(env.LogLevel))]
		if !ok {
			return fmt.Errorf("pkglog: unknown log level: %s", env.LogLevel)
		}
		protolog.SetLevel(protolog.Level(levelValue))
	}
	return nil
}