示例#1
0
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
}
示例#2
0
func init() {
	formatter := log.TextFormatter{}
	formatter.DisableTimestamp = false
	formatter.DisableColors = true
	log.SetFormatter(&formatter)
	//log.SetLevel(log.InfoLevel)
}
示例#3
0
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

}
示例#4
0
func loadLogging() {
	// XXX (mmr) : find a better way to do this
	log = logrus.New()
	fmt := new(logrus.TextFormatter)
	fmt.FullTimestamp = true
	log.Formatter = fmt
	log.Level = logrus.DebugLevel
}
示例#5
0
func init() {
	Log = log.New()
	Log.Level = log.InfoLevel

	formatter := new(log.TextFormatter)
	formatter.FullTimestamp = true
	Log.Formatter = formatter
}
示例#6
0
// LogInit is a utility function to initialize logging
func LogInit(filename string) {
	f, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
	if err != nil {
		log.Fatal("Unable to open Log file ", err)
	}
	Logger.Out = f
	customFormatter := new(logrus.TextFormatter)
	customFormatter.TimestampFormat = "2016-03-28 15:04:05"
	Logger.Formatter = customFormatter
	customFormatter.FullTimestamp = true
	customFormatter.DisableColors = true
}
示例#7
0
func Setup(config Configurer, debug bool) error {
	if logger == nil {
		logger = log.New()
	}

	var err error

	if logger.Level, err = log.ParseLevel(config.Level); err != nil {
		return err
	}

	if debug {
		logger.Level = log.DebugLevel
	}

	switch config.Formatter {
	case "text":
		formatter := new(log.TextFormatter)
		formatter.ForceColors = config.Colored
		formatter.FullTimestamp = true
		logger.Formatter = formatter
		break
	case "json":
		formatter := new(log.JSONFormatter)
		logger.Formatter = formatter
		break
	default:
		return errors.New("Formato de lo log desconocido")
	}

	switch config.Output {
	case "console":
		logger.Out = os.Stdout
		break
	case "file":
		fileName := "mesos-framework-factory.log"
		if config.FileName != "" {
			fileName = config.FileName
		}

		logFile, err := os.OpenFile(fileName, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
		if err != nil {
			logger.Warnln("Error al abrir el archivo")
		}
		logger.Out = logFile
		break
	default:
		return errors.New("Output de logs desconocido")
	}

	return nil
}
示例#8
0
文件: cli.go 项目: ch3lo/crane
func setupLogger(config configuration.Loggging, debug bool) error {
	var err error

	if util.Log.Level, err = log.ParseLevel(config.Level); err != nil {
		return err
	}

	if debug {
		util.Log.Level = log.DebugLevel
	}

	switch config.Formatter {
	case "text":
		formatter := new(log.TextFormatter)
		formatter.ForceColors = config.Colored
		formatter.FullTimestamp = true
		util.Log.Formatter = formatter
		break
	case "json":
		formatter := new(log.JSONFormatter)
		util.Log.Formatter = formatter
		break
	default:
		return errors.New("Formato de lo log desconocido")
	}

	switch config.Output {
	case "console":
		util.Log.Out = os.Stdout
		break
	case "file":
		util.Log.Out = logFile
		break
	default:
		return errors.New("Output de logs desconocido")
	}

	return nil
}
示例#9
0
// makeLogger configures the package level Logger instance
func makeLogger(conf *BaseConfiguration) {
	levelName, err := logrus.ParseLevel(conf.LogLevel)
	if err != nil {
		defer Logger.Warnf("%s is not a valid level. Defaulting to info.", conf.LogLevel)
		levelName = logrus.InfoLevel
	}

	logOut, err := os.OpenFile(conf.LogFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)

	if err != nil {
		defer Logger.Warnf("%s. Defaulting to Stderr.", err)
		logOut = os.Stderr
	}

	formatter := new(logrus.TextFormatter)
	formatter.DisableColors = true
	Logger = logrus.Logger{
		Out:       logOut,
		Formatter: formatter,
		Level:     levelName,
	}
	Logger.Infof("Initialized at level %s", Logger.Level)
}
示例#10
0
文件: log.go 项目: hypertornado/prago
func (m *middlewareLogger) Init(app *App) error {
	err := os.MkdirAll(app.dotPath+"/log", 0777)
	if err != nil && !os.IsExist(err) {
		return err
	}

	m.logger = logrus.New()
	logFormatter := new(logrus.TextFormatter)
	logFormatter.FullTimestamp = true
	m.logger.Formatter = logFormatter
	app.logger = m.logger

	m.file = m.openLogFile(app)
	m.logger.Out = m.file

	app.AddCronTask("remove old log files", func() {
		m.removeLogFiles(app, time.Now().AddDate(0, 0, -7))
	}, func(in time.Time) time.Time {
		return in.Add(1 * time.Hour)
	})

	app.AddCronTask("rotate log files", func() {
		app.Log().Println("Rotating log files")
		if m.file != nil {
			newFile := m.openLogFile(app)
			oldFile := m.file
			m.logger.Out = newFile
			m.file = newFile
			oldFile.Close()
		}
	}, func(in time.Time) time.Time {
		return in.Add(24 * time.Hour)
	})
	app.requestMiddlewares = append(app.requestMiddlewares, requestMiddlewareLogBefore)
	return nil
}
示例#11
0
文件: mount.go 项目: ovh/svfs
func init() {
	configError = config.LoadConfig()

	// Logger
	formatter := new(logrus.TextFormatter)
	formatter.TimestampFormat = time.RFC3339
	formatter.FullTimestamp = true
	logrus.SetFormatter(formatter)
	logrus.SetOutput(os.Stdout)

	// Uid & Gid
	currentUser, err := user.Current()
	if err != nil {
		gid = 0
		uid = 0
	} else {
		// Non-parsable uid & gid should never be seen
		gid, _ = strconv.ParseUint(currentUser.Gid, 10, 64)
		uid, _ = strconv.ParseUint(currentUser.Uid, 10, 64)
	}

	setFlags()
	RootCmd.AddCommand(mountCmd)
}
示例#12
0
文件: app.go 项目: ts33kr/boot
// Build an adequate instance of the structured logger for this
// application instance. The journal builder may draw data from the
// app instance to configure the journal correctly. This method only
// instantiates a very basic journal; anything more complicated than
// that should be implementing using a boot.Provider to do it.
func (app *App) makeJournal(level logrus.Level) *logrus.Logger {
	const m = "begin writing application journal"
	var journal *logrus.Logger = &logrus.Logger{}
	formatter := new(logrus.TextFormatter)  // std
	journal.Level = level                   // use requested level
	journal.Out = os.Stdout                 // all goes to stdout
	journal.Hooks = make(logrus.LevelHooks) // empty
	journal.Formatter = formatter           // set formatter
	formatter.ForceColors = false           // act smart
	formatter.DisableColors = false         // make pretty
	formatter.DisableTimestamp = false      // is useful
	formatter.FullTimestamp = false         // numbers
	formatter.TimestampFormat = time.StampMilli
	formatter.DisableSorting = false // order!
	moment := time.Now().Format(app.TimeLayout)
	journal.WithField("time", moment).Info(m)
	return journal // is ready to use
}
示例#13
0
func init() {
	cobra.OnInitialize(initConfig)
	customFormatter := new(log.TextFormatter)
	customFormatter.FullTimestamp = true
	log.SetFormatter(customFormatter)
}
示例#14
0
func (t *intgFmt) Format(e *log.Entry) ([]byte, error) {
	e.Message = strings.Join([]string{"[INTG-TEST]", e.Message}, " ")
	nt := log.TextFormatter{}
	return nt.Format(e)
}