Пример #1
0
// SetupEnv sets up logging for the given Env.
func SetupEnv(env Env) error {
	var pushers []lion.Pusher
	logAppName := env.LogAppName
	if logAppName == "" {
		logAppName = "app"
	}
	if !env.LogDisableStderr {
		pushers = append(pushers, lion.NewTextWritePusher(os.Stderr))
	}
	if env.CurrentStdout {
		pushers = append(pushers, lion.NewWritePusher(os.Stdout, currentlion.NewMarshaller()))
	}
	if env.LogDirPath != "" {
		pushers = append(pushers, newLogDirPusher(env.LogDirPath, logAppName))
	}
	if env.SyslogNetwork != "" && env.SyslogAddress != "" {
		pusher, err := newSyslogPusher(env.SyslogNetwork, env.SyslogAddress, logAppName)
		if err != nil {
			return err
		}
		pushers = append(pushers, pusher)
	}
	if env.CurrentToken != "" && env.CurrentSyslogAddress != "" {
		pusher, err := currentlion.NewPusher(logAppName, env.CurrentSyslogAddress, env.CurrentToken)
		if err != nil {
			return err
		}
		pushers = append(pushers, pusher)
	}
	switch len(pushers) {
	case 0:
		lion.SetLogger(lion.DiscardLogger)
	case 1:
		lion.SetLogger(lion.NewLogger(pushers[0]))
	default:
		lion.SetLogger(lion.NewLogger(lion.NewMultiPusher(pushers...)))
	}
	lion.RedirectStdLogger()
	if env.LogLevel != "" {
		level, err := lion.NameToLevel(strings.ToUpper(env.LogLevel))
		if err != nil {
			return err
		}
		lion.SetLevel(level)
	}
	return nil
}
Пример #2
0
// SetupLogging sets up logging.
func SetupLogging(appName string, env Env) error {
	var pushers []lion.Pusher
	if !env.DisableStderrLog {
		pushers = append(
			pushers,
			lion.NewTextWritePusher(
				os.Stderr,
			),
		)
	}
	if env.LogDir != "" {
		pushers = append(
			pushers,
			lion.NewTextWritePusher(
				&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,
			sysloglion.NewPusher(
				writer,
			),
		)
	}
	if len(pushers) > 0 {
		lion.SetLogger(
			lion.NewLogger(
				lion.NewMultiPusher(
					pushers...,
				),
			),
		)
	} else {
		lion.SetLogger(
			lion.DiscardLogger,
		)
	}
	lion.RedirectStdLogger()
	if env.LogLevel != "" {
		level, err := lion.NameToLevel(strings.ToUpper(env.LogLevel))
		if err != nil {
			return err
		}
		lion.SetLevel(level)
	}
	return nil
}