Exemple #1
0
func newLogDirPusher(logDirPath string, logAppName string) lion.Pusher {
	return lion.NewTextWritePusher(
		&lumberjack.Logger{
			Filename:   filepath.Join(logDirPath, fmt.Sprintf("%s.log", logAppName)),
			MaxBackups: 3,
		},
	)
}
Exemple #2
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
}
Exemple #3
0
func testRoundTripAndTextMarshaller(t *testing.T, f func(protolion.Logger), expected string) {
	for _, marshalPair := range []struct {
		marshaller   lion.Marshaller
		unmarshaller lion.Unmarshaller
	}{
		{
			protolion.DelimitedMarshaller,
			protolion.DelimitedUnmarshaller,
		},
		{
			protolion.Base64DelimitedMarshaller,
			protolion.Base64DelimitedUnmarshaller,
		},
	} {
		buffer := bytes.NewBuffer(nil)
		fakeTimer := newFakeTimer(0)
		logger := protolion.NewLogger(
			lion.NewLogger(
				lion.NewWritePusher(
					buffer,
					marshalPair.marshaller,
				),
				lion.LoggerWithIDAllocator(newFakeIDAllocator()),
				lion.LoggerWithTimer(fakeTimer),
			),
		)
		f(logger)
		puller := lion.NewReadPuller(
			buffer,
			marshalPair.unmarshaller,
		)
		writeBuffer := bytes.NewBuffer(nil)
		writePusher := lion.NewTextWritePusher(
			writeBuffer,
			lion.TextMarshallerDisableTime(),
		)
		for encodedEntry, pullErr := puller.Pull(); pullErr != io.EOF; encodedEntry, pullErr = puller.Pull() {
			require.NoError(t, pullErr)
			entry, err := encodedEntry.Decode()
			require.NoError(t, err)
			require.NoError(t, writePusher.Push(entry))
		}
		require.Equal(t, expected, writeBuffer.String())
	}
}
Exemple #4
0
func testRoundTripAndTextMarshallerTail(t *testing.T, f func(protolion.Logger), expected string) {
	file, err := ioutil.TempFile("", "lion")
	require.NoError(t, err)
	filePath := file.Name()
	// will not actually get called if a require statement is hit
	defer func() {
		_ = file.Close()
		_ = os.Remove(filePath)

	}()
	fakeTimer := newFakeTimer(0)
	logger := protolion.NewLogger(
		lion.NewLogger(
			lion.NewWritePusher(
				file,
				protolion.Base64DelimitedNewlineMarshaller,
			),
			lion.LoggerWithIDAllocator(newFakeIDAllocator()),
			lion.LoggerWithTimer(fakeTimer),
		),
	)
	f(logger)
	writeBuffer := bytes.NewBuffer(nil)
	writePusher := lion.NewTextWritePusher(
		writeBuffer,
		lion.TextMarshallerDisableTime(),
	)
	require.NoError(
		t,
		taillion.Tail(
			filePath,
			protolion.Base64DelimitedUnmarshaller,
			writePusher.Push,
			nil,
			taillion.TailOptions{},
		),
	)
	require.Equal(t, expected, writeBuffer.String())
}
Exemple #5
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
}