func (log *Logger) Init(logFile string, logOut bool, logLevel string) {
	var file *os.File
	customFormatter := new(logrus.TextFormatter)
	customFormatter.TimestampFormat = "2006-01-02 15:04:05"
	customFormatter.ForceColors = true
	customFormatter.FullTimestamp = true
	log.logger.SetOutput(os.Stderr)
	log.logger.SetFormatter(customFormatter)

	// #1837 - trigger the console to use color-mode since it won't be
	// otherwise triggered until the first log entry
	// this is covers the situation where the logger is only logging to file
	// and therefore does not trigger the console color-mode - resulting in
	// the access log colouring not being applied
	_, _ = customFormatter.Format(logrus.NewEntry(log.logger))

	if logFile != "" {
		var err error
		file, err = os.OpenFile(logFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)

		if err != nil {
			fmt.Printf("Could not open '%s' for log output due to error: %s\n", logFile, err.Error())
		}
	}

	if file != nil {
		if logOut {
			// log to file separately disabling colours
			fileFormatter := new(logrus.TextFormatter)
			fileFormatter.TimestampFormat = customFormatter.TimestampFormat
			fileFormatter.FullTimestamp = customFormatter.FullTimestamp
			log.logger.AddHook(&fileLogHook{
				Writer:    file,
				Formatter: fileFormatter,
			})
		} else {
			// logging to file only
			// turn off the colouring for the file
			customFormatter.ForceColors = false
			log.logger.Out = file
		}
	}

	// otherwise, output to StdErr

	log.SetLogLevel(logLevel)
}
// Init initialises the logger based on a logging configuration
Init initialises the logger based on a logging configuration
// Init initialises the logger based on a logging configuration
func (log *Logger) Init(logFile string, logOut bool, logLevel string) {
	var file *os.File
	customFormatter := new(logrus.TextFormatter)
	customFormatter.TimestampFormat = "2006-01-02 15:04:05"
	customFormatter.ForceColors = true
	customFormatter.FullTimestamp = true
	log.logger.SetOutput(os.Stderr)
	log.logger.SetFormatter(customFormatter)
	_, _ = customFormatter.Format(logrus.NewEntry(log.logger))
	if logFile != "" {
		var err error
		file, err = os.OpenFile(logFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
		if err != nil {
			fmt.Printf("Could not open '%s' for log output due to error: %s\n", logFile, err.Error())
		}
	}
	if file != nil {
		if logOut {
			fileFormatter := new(logrus.TextFormatter)
			fileFormatter.TimestampFormat = customFormatter.TimestampFormat
			fileFormatter.FullTimestamp = customFormatter.FullTimestamp
			log.logger.AddHook(&fileLogHook{
				Writer:    file,
				Formatter: fileFormatter,
			})
		} else {
			customFormatter.ForceColors = false
			log.logger.Out = file
		}
	}
	log.SetLogLevel(logLevel)
}
func ( log * Logger ) Init ( logFile string , logOut bool , logLevel string ) { var file * os . File customFormatter := new ( logrus . TextFormatter ) customFormatter . TimestampFormat = " " customFormatter . ForceColors = true customFormatter . FullTimestamp = true log . logger . SetOutput ( os . Stderr ) log . logger . SetFormatter ( customFormatter ) _ , _ = customFormatter . Format ( logrus . NewEntry ( log . logger ) ) if logFile != " " { var err error file , err = os . OpenFile ( logFile , os . O_CREATE | os . O_WRONLY | os . O_APPEND , 0644 ) if err != nil { fmt . Printf ( " \n " , logFile , err . Error ( ) ) } } if file != nil { if logOut { fileFormatter := new ( logrus . TextFormatter ) fileFormatter . TimestampFormat = customFormatter . TimestampFormat fileFormatter . FullTimestamp = customFormatter . FullTimestamp log . logger . AddHook ( & fileLogHook { Writer : file , Formatter : fileFormatter , } ) } else { customFormatter . ForceColors = false log . logger . Out = file } } log . SetLogLevel ( logLevel ) }
Init initialises the logger based on a logging configuration
Init initialises the logger based on a logging configuration
// #1837 - trigger the console to use color-mode since it won't be // otherwise triggered until the first log entry // this is covers the situation where the logger is only logging to file // and therefore does not trigger the console color-mode - resulting in // the access log colouring not being applied // log to file separately disabling colours // logging to file only // turn off the colouring for the file // otherwise, output to StdErr