Ejemplo n.º 1
0
// New returns configured ironsmile™ logger that is ready to use.
func New(cfg config.LoggerSection) (*logger.Logger, error) {
	logger := logger.New()
	var s settings
	err := json.Unmarshal(cfg.Settings, &s)
	if err != nil {
		return nil, fmt.Errorf("Error while parsing logger settings for 'ironsmile_logger':\n%s\n", err)
	}

	var errorOutput, debugOutput, logOutput io.Writer

	if s.DebugFile != "" {
		debugOutput, err = os.OpenFile(s.DebugFile, 0, 7770)
		if err != nil {
			return nil, fmt.Errorf("Error while opening file [%s] for debug output:\n%s\n", s.DebugFile, err)
		}
		logger.SetDebugOutput(debugOutput)
	}

	if s.LogFile != "" {
		logOutput, err = os.OpenFile(s.LogFile, 0, 7770)
		if err != nil {
			return nil, fmt.Errorf("Error while opening file [%s] for log output:\n%s\n", s.LogFile, err)
		}
		logger.SetLogOutput(logOutput)
	} else if debugOutput != nil {
		logger.SetLogOutput(debugOutput)
	}

	if s.ErrorFile != "" {
		errorOutput, err = os.OpenFile(s.ErrorFile, 0, 7770)
		if err != nil {
			return nil, fmt.Errorf("Error while opening file [%s] for error output:\n%s\n", s.ErrorFile, err)
		}
		logger.SetErrorOutput(errorOutput)
	} else if logOutput != nil {
		logger.SetErrorOutput(logOutput)
	}

	return logger, nil
}
Ejemplo n.º 2
0
Archivo: impl.go Proyecto: na--/nedomi
// New returns configured ironsmile™ logger that is ready to use.
// Configuration:
// 	error	a path to a file to log calls to Errorf?
// 	log	a path to a file to log calls to Logf?
// 	debug	a path to a file to log calls to Debugf?
//
// If debug is set but log not, debug's file will be used for log.
// If log is set(either through the configuration or copied from  debug),
// but error is not, error will be set to log's file.
// The files are appended to if existing, not truncated.
func New(cfg *config.Logger) (*logger.Logger, error) {
	if len(cfg.Settings) < 1 {
		return nil, fmt.Errorf("logger 'settings' key is missing")
	}

	l := logger.New()
	var s settings
	err := json.Unmarshal(cfg.Settings, &s)
	if err != nil {
		return nil, fmt.Errorf("error while parsing logger settings: %s", err)
	}

	var errorOutput, debugOutput, logOutput io.Writer

	if s.DebugFile != "" {
		debugOutput, err = os.OpenFile(s.DebugFile, logFileFlags, logFilePerms)
		if err != nil {
			return nil, fmt.Errorf("error while opening file [%s] for debug output: %s",
				s.DebugFile, err)
		}
		l.SetDebugOutput(debugOutput)
	}

	if s.LogFile != "" {
		logOutput, err = os.OpenFile(s.LogFile, logFileFlags, logFilePerms)
		if err != nil {
			return nil, fmt.Errorf("error while opening file [%s] for log output: %s",
				s.LogFile, err)
		}
	} else if debugOutput != nil {
		logOutput = debugOutput
	}

	if logOutput != nil {
		l.SetLogOutput(logOutput)
	}
	if s.ErrorFile != "" {
		errorOutput, err = os.OpenFile(s.ErrorFile, logFileFlags, logFilePerms)
		if err != nil {
			return nil, fmt.Errorf("Error while opening file [%s] for error output: %s",
				s.ErrorFile, err)
		}
	} else if logOutput != nil {
		errorOutput = logOutput
	}
	if errorOutput != nil {
		l.SetErrorOutput(errorOutput)
	}

	if debugOutput != nil {
		l.Level = logger.LevelDebug
	} else if logOutput != nil {
		l.Level = logger.LevelLog
	} else if errorOutput != nil {
		l.Level = logger.LevelError
	} else {
		return nil, fmt.Errorf("ironsmile logger needs at least one file to log to")
	}

	return l, nil
}