Esempio n. 1
0
func setupLogger(b *testing.B) (string, *os.File, protolog.Logger) {
	tempDir, err := ioutil.TempDir("", "protolog")
	require.NoError(b, err)
	file, err := os.Create(filepath.Join(tempDir, "log.out"))
	require.NoError(b, err)
	logger := protolog.NewLogger(
		protolog.NewWritePusher(
			protolog.NewFileFlusher(file),
			protolog.WritePusherOptions{},
		),
		protolog.LoggerOptions{},
	).AtLevel(protolog.Level_LEVEL_INFO)
	return tempDir, file, logger
}
Esempio n. 2
0
func init() {
	dlog.SetLogger(NewLogger(protolog.NewStandardLogger(protolog.NewFileFlusher(os.Stderr))))
}
Esempio n. 3
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
}