本文整理汇总了Golang中github.com/Sirupsen/logrus.SetFormatter函数的典型用法代码示例。如果您正苦于以下问题:Golang SetFormatter函数的具体用法?Golang SetFormatter怎么用?Golang SetFormatter使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了SetFormatter函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: NewLogrusLogger
func NewLogrusLogger(ctx *cli.Context) Logger {
logFile := ctx.GlobalString("log-file")
if logFile != "" {
if err := os.MkdirAll(path.Dir(logFile), 0755); err != nil {
logrus.Errorf("Failed to create path %s: %s", path.Dir(logFile), err.Error())
} else {
file, err := os.OpenFile(logFile, syscall.O_CREAT|syscall.O_APPEND|syscall.O_WRONLY, 0644)
if err != nil {
logrus.Errorf("Failed to open log file %s: %s", logFile, err.Error())
} else {
logrus.SetOutput(file)
}
}
}
switch ctx.GlobalString("log-format") {
case "json":
logrus.SetFormatter(&logrus.JSONFormatter{})
case "logstash":
logrus.SetFormatter(&logstash.LogstashFormatter{})
default:
logrus.SetFormatter(&logrus.TextFormatter{})
}
if ctx.GlobalBool("debug") {
logrus.SetLevel(logrus.DebugLevel)
}
return &logrusLogger{
logger: logrus.StandardLogger(),
}
}
示例2: ConfigureLogging
func ConfigureLogging(level, format string, output io.Writer) error {
switch level {
case "debug":
log.SetLevel(log.DebugLevel)
case "info":
log.SetLevel(log.InfoLevel)
case "warn":
log.SetLevel(log.WarnLevel)
case "error":
log.SetLevel(log.ErrorLevel)
case "fatal":
log.SetLevel(log.FatalLevel)
case "panic":
log.SetLevel(log.PanicLevel)
default:
return errors.New("Invalid log level: " + level)
}
switch format {
case "text":
log.SetFormatter(&log.TextFormatter{
TimestampFormat: time.RFC3339,
FullTimestamp: true,
})
case "json":
log.SetFormatter(&log.JSONFormatter{
TimestampFormat: time.RFC3339,
})
default:
return errors.New("Invalid format type: " + format)
}
log.SetOutput(output)
return nil
}
示例3: Manage
func (service *Service) Manage() (string, error) {
usage := "Usage: metronome install | remove | start | stop | status | agent\n"
if flag.NArg() > 0 {
switch flag.Args()[0] {
case "install":
return service.Install()
case "remove":
return service.Remove()
case "start":
return service.Start()
case "stop":
return service.Stop()
case "status":
return service.Status()
case "agent":
return agent()
case "push":
return scheduler.Push()
case "dispatch":
return dispatch(flag.Args()[1])
case "version":
log.SetFormatter(&util.SimpleFormatter{})
return fmt.Sprintf("metronome %s\n", Version), nil
default:
log.SetFormatter(&util.SimpleFormatter{})
return usage, nil
}
}
return agent()
}
示例4: configureLogging
func configureLogging(v *viper.Viper) {
level, err := log.ParseLevel(v.GetString("log_level"))
if err != nil {
log.Fatalln(err)
}
log.SetLevel(level)
if v.GetString("log_format") == "text" {
log.SetFormatter(&log.TextFormatter{DisableColors: true, FullTimestamp: true})
} else if v.GetString("log_format") == "json" {
log.SetFormatter(&log.JSONFormatter{})
} else {
log.Errorln("Error: log_type invalid, defaulting to text")
log.SetFormatter(&log.TextFormatter{})
}
switch v.GetString("log_target") {
case "stdout":
log.SetOutput(os.Stdout)
case "stderr":
log.SetOutput(os.Stderr)
default:
log.Errorln("Error: log_target invalid, defaulting to Stdout")
log.SetOutput(os.Stdout)
}
}
示例5: run
func run() {
fmtlog.SetFlags(fmtlog.Lshortfile | fmtlog.LstdFlags)
// load global configuration
globalConfiguration := LoadConfiguration()
loggerMiddleware := middlewares.NewLogger(globalConfiguration.AccessLogsFile)
defer loggerMiddleware.Close()
// logging
level, err := log.ParseLevel(strings.ToLower(globalConfiguration.LogLevel))
if err != nil {
log.Fatal("Error getting level", err)
}
log.SetLevel(level)
if len(globalConfiguration.TraefikLogsFile) > 0 {
fi, err := os.OpenFile(globalConfiguration.TraefikLogsFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
defer fi.Close()
if err != nil {
log.Fatal("Error opening file", err)
} else {
log.SetOutput(fi)
log.SetFormatter(&log.TextFormatter{DisableColors: true, FullTimestamp: true, DisableSorting: true})
}
} else {
log.SetFormatter(&log.TextFormatter{FullTimestamp: true, DisableSorting: true})
}
log.Debugf("Global configuration loaded %+v", globalConfiguration)
server := NewServer(*globalConfiguration)
server.Start()
defer server.Close()
log.Info("Shutting down")
}
示例6: InitLog
// InitLog initializes the logrus logger
func InitLog(logLevel, formatter string) error {
switch formatter {
case LogStashFormatter:
logrus.SetFormatter(&logstash.LogstashFormatter{
TimestampFormat: time.RFC3339,
})
default:
logrus.SetFormatter(&logrus.TextFormatter{
ForceColors: true,
FullTimestamp: true,
})
}
logrus.SetOutput(os.Stdout)
level, err := logrus.ParseLevel(logLevel)
if err != nil {
logrus.SetLevel(logrus.DebugLevel)
return err
}
logrus.SetLevel(level)
return nil
}
示例7: ResponseLogAndError
// ResponseLogAndError would log the error before call ResponseError()
func ResponseLogAndError(v interface{}) {
if e, ok := v.(*logrus.Entry); ok {
e.Error(e.Message)
oldFormatter := e.Logger.Formatter
logrus.SetFormatter(&logrus.JSONFormatter{})
s, err := e.String()
logrus.SetFormatter(oldFormatter)
if err != nil {
ResponseError(err.Error())
return
}
// Cosmetic since " would be escaped
ResponseError(strings.Replace(s, "\"", "'", -1))
} else {
e, isErr := v.(error)
_, isRuntimeErr := e.(runtime.Error)
if isErr && !isRuntimeErr {
logrus.Errorf(fmt.Sprint(e))
ResponseError(fmt.Sprint(e))
} else {
logrus.Errorf("Caught FATAL error: %s", v)
debug.PrintStack()
ResponseError("Caught FATAL error: %s", v)
}
}
}
示例8: NewCustomizedLogger
func NewCustomizedLogger(level string, developerModeFlag bool) (*log.Logger, error) {
logger := log.New()
lv, err := log.ParseLevel(level)
if err != nil {
return nil, err
}
logger.Level = lv
if developerModeFlag {
customFormatter := new(log.TextFormatter)
customFormatter.FullTimestamp = true
customFormatter.TimestampFormat = "2006-01-02 15:04:05"
log.SetFormatter(customFormatter)
log.SetLevel(log.DebugLevel)
logger.Level = lv
logger.Formatter = customFormatter
} else {
customFormatter := new(log.JSONFormatter)
customFormatter.TimestampFormat = "2006-01-02 15:04:05"
log.SetFormatter(customFormatter)
customFormatter.DisableTimestamp = false
log.SetLevel(log.InfoLevel)
logger.Level = lv
logger.Formatter = customFormatter
}
logger.Out = os.Stdout
return logger, nil
}
示例9: InitializeLogger
func InitializeLogger(developerModeFlag bool) {
logger = log.New()
if developerModeFlag {
customFormatter := new(log.TextFormatter)
customFormatter.FullTimestamp = true
customFormatter.TimestampFormat = "2006-01-02 15:04:05"
log.SetFormatter(customFormatter)
log.SetLevel(log.DebugLevel)
logger.Level = log.DebugLevel
logger.Formatter = customFormatter
} else {
customFormatter := new(log.JSONFormatter)
customFormatter.TimestampFormat = "2006-01-02 15:04:05"
log.SetFormatter(customFormatter)
customFormatter.DisableTimestamp = false
log.SetLevel(log.InfoLevel)
logger.Level = log.InfoLevel
logger.Formatter = customFormatter
}
logger.Out = os.Stdout
}
示例10: InitializeLogging
func InitializeLogging(config *LoggingConfig, useStd bool, level string) {
var lvl = log.DebugLevel
var e error
if level != "" {
lvl, e = log.ParseLevel(level)
} else if config.Level != "" {
lvl, e = log.ParseLevel(config.Level)
}
if nil != e {
lvl = log.DebugLevel
}
if useStd {
log.SetOutput(os.Stdout)
OUTPUT_FILE = nil
} else {
f, e := os.OpenFile(config.Filename, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666)
if nil != e {
fmt.Errorf("Open file <%s> for logging failed<%v>!\n", config.Filename, e)
} else {
log.SetOutput(f)
OUTPUT_FILE = f
}
}
if strings.ToLower(config.Format) == "json" {
log.SetFormatter(&log.JSONFormatter{})
} else {
log.SetFormatter(&PlainFormatter{})
}
log.SetLevel(lvl)
//log.Info("Logging Initialized.")
}
示例11: main
func main() {
// configure logging
logrus.SetLevel(logrus.InfoLevel)
logrus.SetFormatter(&logrus.TextFormatter{FullTimestamp: true})
// options to change log level
globalOptions.Quiet = func() {
logrus.SetLevel(logrus.WarnLevel)
}
globalOptions.Verbose = func() {
logrus.SetLevel(logrus.DebugLevel)
}
globalOptions.LogJSON = func() {
logrus.SetFormatter(&logrus.JSONFormatter{})
}
globalOptions.LogSyslog = func() {
setupSyslog()
}
originalArgs = os.Args
if _, err := parser.Parse(); err != nil {
os.Exit(1)
}
}
示例12: setupLogging
func setupLogging() {
switch viper.GetString("log-level") {
case "debug":
log.SetLevel(log.DebugLevel)
case "info":
log.SetLevel(log.InfoLevel)
case "warn":
log.SetLevel(log.WarnLevel)
case "error":
log.SetLevel(log.ErrorLevel)
case "fatal":
log.SetLevel(log.FatalLevel)
default:
log.WithField("log-level", viper.GetString("log-level")).Warning("invalid log level. defaulting to info.")
log.SetLevel(log.InfoLevel)
}
switch viper.GetString("log-format") {
case "text":
log.SetFormatter(new(log.TextFormatter))
case "json":
log.SetFormatter(new(log.JSONFormatter))
default:
log.WithField("log-format", viper.GetString("log-format")).Warning("invalid log format. defaulting to text.")
log.SetFormatter(new(log.TextFormatter))
}
}
示例13: initLogger
func initLogger() {
if strings.ToUpper(config.OutputFormat) == "JSON" {
log.SetFormatter(&log.JSONFormatter{})
} else {
// The TextFormatter is default, you don't actually have to do this.
log.SetFormatter(&log.TextFormatter{})
}
}
示例14: ConfigureLogger
func ConfigureLogger() {
switch viper.GetString("ENV") {
case "DEVELOPMENT":
log.SetFormatter(&log.TextFormatter{ForceColors: true})
case "PRODUCTION":
log.SetFormatter(&log.JSONFormatter{})
default:
log.SetFormatter(&log.TextFormatter{})
}
}
示例15: configureLogging
// configureLogging prepares the context with a logger using the
// configuration.
func configureLogging(ctx context.Context, config *configuration.Configuration) (context.Context, error) {
if config.Log.Level == "" && config.Log.Formatter == "" {
// If no config for logging is set, fallback to deprecated "Loglevel".
log.SetLevel(logLevel(config.Loglevel))
ctx = context.WithLogger(ctx, context.GetLogger(ctx, "version"))
return ctx, nil
}
log.SetLevel(logLevel(config.Log.Level))
formatter := config.Log.Formatter
if formatter == "" {
formatter = "text" // default formatter
}
switch formatter {
case "json":
log.SetFormatter(&log.JSONFormatter{
TimestampFormat: time.RFC3339Nano,
})
case "text":
log.SetFormatter(&log.TextFormatter{
TimestampFormat: time.RFC3339Nano,
})
case "logstash":
log.SetFormatter(&logstash.LogstashFormatter{
TimestampFormat: time.RFC3339Nano,
})
default:
// just let the library use default on empty string.
if config.Log.Formatter != "" {
return ctx, fmt.Errorf("unsupported logging formatter: %q", config.Log.Formatter)
}
}
if config.Log.Formatter != "" {
log.Debugf("using %q logging formatter", config.Log.Formatter)
}
// log the application version with messages
ctx = context.WithLogger(ctx, context.GetLogger(ctx, "version"))
if len(config.Log.Fields) > 0 {
// build up the static fields, if present.
var fields []interface{}
for k := range config.Log.Fields {
fields = append(fields, k)
}
ctx = context.WithValues(ctx, config.Log.Fields)
ctx = context.WithLogger(ctx, context.GetLogger(ctx, fields...))
}
return ctx, nil
}