Example #1
0
func (lc *logConfig) createContexts(params map[string]string) {
	// collect settings for all contexts from config
	ctxs := groupConfigParams(params, cfgContext, isCorrectLoggerName)

	// create contexts
	for logName, ctxAttributes := range ctxs {
		appenders := lc.getAppendersFromList(ctxAttributes[cfgContextAppenders])
		if len(appenders) == 0 {
			panic("Context \"" + logName + "\" doesn't refer to at least one declared appender.")
		}

		levelName := ctxAttributes[cfgContextLevel]
		level := INFO
		if len(levelName) > 0 {
			level = lc.getLevelByName(levelName)
			if level < 0 {
				panic("Unknown log level \"" + levelName + "\" for context \"" + logName + "\"")
			}
		}

		bufSize, err := gorivets.ParseInt64(ctxAttributes[cfgContextBufSize], 0, 100000, 100)
		if err != nil {
			panic("Incorrect buffer size=" + ctxAttributes[cfgContextBufSize] +
				" value for context \"" + logName + "\" should be positive integer: " + err.Error())
		}

		inh, err := gorivets.ParseBool(ctxAttributes[cfgContextInherited], true)
		if err != nil {
			panic("Incorrect context attibute " + cfgContextInherited + " value, should be true or false")
		}

		blocking, err := gorivets.ParseBool(ctxAttributes[cfgContextBlocking], true)
		if err != nil {
			panic("Incorrect context attibute " + cfgContextBlocking + " value, should be true or false")
		}

		setLogLevel(level, logName, lc.logLevels)
		context, _ := newLogContext(logName, appenders, inh, blocking, int(bufSize))
		lc.logContexts.Add(context)
	}
}
Example #2
0
func (faf *fileAppenderFactory) NewAppender(params map[string]string) (Appender, error) {
	layout, ok := params[FAParamLayout]
	if !ok || len(layout) == 0 {
		return nil, errors.New("Cannot create file appender: layout should be specified")
	}

	fileName, ok := params[FAParamFileName]
	if !ok || len(fileName) == 0 {
		return nil, errors.New("Cannot create file appender: file should be specified")
	}

	layoutTemplate, err := ParseLayout(layout)
	if err != nil {
		return nil, errors.New("Cannot create file appender, incorrect layout: " + err.Error())
	}

	buffer, err := gorivets.ParseInt(params[FAParamFileBuffer], 1, 10000, 100)
	if err != nil {
		return nil, errors.New("Invalid " + FAParamFileBuffer + " value: " + err.Error())
	}

	fileAppend, err := gorivets.ParseBool(params[FAParamFileAppend], true)
	if err != nil {
		return nil, errors.New("Invalid " + FAParamFileAppend + " value: " + err.Error())
	}

	maxFileSize, err := gorivets.ParseInt64(params[FAParamMaxSize], 1000, maxInt64, maxInt64)
	if err != nil {
		return nil, errors.New("Invalid " + FAParamMaxSize + " value: " + err.Error())
	}

	maxDiskSpace, err := gorivets.ParseInt64(params[FAParamMaxDiskSpace], 2000, maxInt64, maxInt64)
	if err != nil {
		return nil, errors.New("Invalid " + FAParamMaxDiskSpace + " value: " + err.Error())
	}

	rotateStr, ok := params[FAParamRotate]
	rotateStr = strings.Trim(rotateStr, " ")
	rState := rsNone
	if ok && len(rotateStr) > 0 {
		rState, ok = rotateState[rotateStr]
		if !ok {
			return nil, errors.New("Unknown rotate state \"" + rotateStr +
				"\", expected \"none\", \"size\", or \"daily \" value")
		}
	}

	if maxDiskSpace/2 < maxFileSize && rState != rsNone {
		return nil, errors.New("Invalid " + FAParamMaxDiskSpace +
			" value. It should be at least twice bigger than " + FAParamMaxSize)
	}

	app := &fileAppender{}
	app.msgChannel = make(chan string, buffer)
	app.controlCh = make(chan bool, 1)
	app.layoutTemplate = layoutTemplate
	app.fileName = fileName
	app.fileAppend = fileAppend
	app.maxSize = maxFileSize
	app.maxDiskSpace = maxDiskSpace
	app.rotate = rState
	app.stat.chunks, app.stat.chunksSize = app.getLogChunks()

	go func() {
		defer app.close()
		app.stat.startTime = time.Now()
		for {
			str, ok := <-app.msgChannel
			if !ok {
				break
			}

			if app.isRotationNeeded() {
				app.rotateFile()
			}
			app.writeMsg(str)
		}
	}()
	return app, nil
}