func dateTimeFormat() {
	fmt.Println("Date time format")

	testConfig := `
<seelog type="sync">
	<outputs formatid="main">
		<console/>
	</outputs>
	<formats>
		<format id="main" format="%Date/%Time [%LEV] %Msg%n"/>
	</formats>
</seelog>`

	logger, err := log.LoggerFromConfigAsBytes([]byte(testConfig))

	if err != nil {
		fmt.Println(err)
	}

	loggerErr := log.ReplaceLogger(logger)

	if loggerErr != nil {
		fmt.Println(loggerErr)
	}

	log.Trace("Test message!")
}
Exemple #2
0
// path can be ""
func InitLog(path string, debug bool) {

	config := `
<seelog type="sync" minlevel="${minlevel}">
    <outputs formatid="main">
        <console/>
        <!-- ${extra} -->
    </outputs>
    <formats>
        <format id="main" format="[EQ-%LEV] %Date(15:04:05.00): %Msg (at %File:%Line) %n"/>
    </formats>
</seelog>`

	if path != "" {
		extra := fmt.Sprintf("<file path=\"%s\"/>", path)
		config = strings.Replace(config, "<!-- ${extra} -->",
			extra, -1)
	}

	Debug = debug
	if debug {
		config = strings.Replace(config, "${minlevel}", "debug", -1)
	} else {
		config = strings.Replace(config, "${minlevel}", "info", -1)
	}

	logger, err := seelog.LoggerFromConfigAsBytes([]byte(config))
	if err != nil {
		panic(err)
	}
	seelog.ReplaceLogger(logger)
	if path != "" {
		seelog.Debugf("Initialized the logger for %s", path)
	}
}
Exemple #3
0
func NewLogs() {
	logger, err := log.LoggerFromConfigAsBytes([]byte(default_template))
	if err != nil {
		log.Criticalf("log config err: %v", err)
	}
	log.ReplaceLogger(logger)
}
Exemple #4
0
func configLogger() {
	config := `
<seelog type="sync">
	<outputs>
		<filter levels="trace">
			<console formatid="skipped"/>
		</filter>
		<filter levels="debug">
			<console formatid="unchanged"/>
		</filter>
		<filter levels="info">
			<console formatid="plain"/>
		</filter>
		<filter levels="warn,error,critical">
			<console formatid="error"/>
		</filter>
	</outputs>
	<formats>
		<format id="error" format="%EscM(31)%Msg%EscM(0)%n"/>
		<format id="plain" format="%Msg%n"/>
		<format id="unchanged" format="%EscM(32)%Msg%EscM(0)%n"/>
		<format id="skipped" format="%EscM(34)%Msg%EscM(0)%n"/>
	</formats>
</seelog>`

	logger, err := log.LoggerFromConfigAsBytes([]byte(config))
	if err != nil {
		panic(err)
	}
	log.ReplaceLogger(logger)
}
func configureLog() {
	level := Get("lamp.log.level")
	if level == "" {
		level = "warn"
		printVerboseMessage("Could not get log level from configuration, will use default \"warn\".")
	}

	logDir := os.Getenv(logDir)

	var outPath string
	logFile := Get("lamp.log.file")
	if logFile == "" {
		logFile = "lamp.log"
		printVerboseMessage("Could not get log filename from configuration. \"lamp.log\" will be used as log filename.")
	}
	if logDir != "" {
		outPath = logDir + sep + logFile
		printVerboseMessage("Will write logs to: \n" + outPath)
	} else {
		outPath = lampHome() + "logs" + sep + logFile
		printVerboseMessage("LAMP_LOGS_DIR environment variable is not set. Will write logs to: \n" + outPath)
	}

	logConfig := template(outPath, level)
	logger, err := seelog.LoggerFromConfigAsBytes([]byte(logConfig))
	if err != nil {
		fmt.Printf("Error occured while configuring logger: %s\n", err.Error())
	}
	log.UseLogger(logger)
}
func testFuncException() {
	fmt.Println("testFuncException")
	testConfig := `
<seelog type="sync" minlevel="info">
	<exceptions>
		<exception funcpattern="*main.test*Except*" minlevel="error"/>
	</exceptions>
	<outputs>
		<console/>
	</outputs>
</seelog>`

	logger, _ := log.LoggerFromConfigAsBytes([]byte(testConfig))
	log.ReplaceLogger(logger)

	log.Trace("NOT Printed")
	log.Debug("NOT Printed")
	log.Info("NOT Printed")
	log.Warn("NOT Printed")
	log.Error("Printed")
	log.Critical("Printed")

	log.Current.Trace("NOT Printed")
	log.Current.Debug("NOT Printed")
	log.Current.Info("NOT Printed")
	log.Current.Warn("NOT Printed")
	log.Current.Error("Printed")
	log.Current.Critical("Printed")
}
Exemple #7
0
// ConfigureLogger configures the new logger according to the configuration and sets it as the current logger.
func ConfigureLogger(testConfig []byte) {
	loggr, err := seelog.LoggerFromConfigAsBytes([]byte(testConfig))
	if err != nil {
		fmt.Printf("error occured: %s\n", err.Error())
	}
	UseLogger(loggr)
}
Exemple #8
0
func setup_logging() {
	level := "info"
	if *g_debug == true {
		level = "debug"
	}
	config := fmt.Sprintf(`
<seelog type="sync" minlevel="%s">
	<outputs formatid="main">
		<console/>
	</outputs>
	<formats>
		<format id="main" format="%%Date %%Time [%%LEVEL] %%File|%%FuncShort|%%Line: %%Msg%%n"/>
	</formats>
</seelog>`, level)

	logger, err := log.LoggerFromConfigAsBytes([]byte(config))

	if err != nil {
		fmt.Println("Failed to config logging:", err)
		os.Exit(1)
	}

	log.ReplaceLogger(logger)

	log.Info("Logging config is successful")
}
func TestGosnmp(t *testing.T) {
	RegisterFailHandler(Fail)
	pid := os.Getpid()
	loggingConfig := fmt.Sprintf(`
<seelog>
    <outputs>
        <buffered size="10000" flushperiod="1000" formatid="debug">
            <rollingfile type="size" filename="logs/debuglog.log" maxsize="10000000" maxrolls="10" />
        </buffered>
    </outputs>
    <formats>
    	<format id="debug" format="%d %%Date(Mon Jan _2 15:04:05.000000) [%%LEV] %%File:%%Line %%Msg%%n"/>
    </formats>
</seelog>`, pid)
	logger, err := seelog.LoggerFromConfigAsBytes([]byte(loggingConfig))
	if err != nil {
		Fail(fmt.Sprintf("Couldn't initialize logger: %s", err))
	}
	defer logger.Flush()
	testIdGenerator := make(chan string)
	go func() {
		for i := 0; ; i++ {
			testIdGenerator <- fmt.Sprintf("test %-3d", i)
		}
	}()
	setupV2cClientTest(logger, testIdGenerator)
	SetupLowLevelContextTest(logger, testIdGenerator)
	RunSpecs(t, "gosnmp Suite")
}
func SetupTestLogging() {
	var appConfig = `
  <seelog type="sync" minlevel='%s'>
  <outputs formatid="scanner">
    <filter levels="critical,error,warn,info">
      <console formatid="scanner" />
    </filter>
    <filter levels="debug,trace">
      <console formatid="debug" />
    </filter>
  </outputs>
  <formats>
  <format id="scanner" format="test: [%%LEV] %%Msg%%n" />
  <format id="debug" format="test: [%%LEV] %%Func :: %%Msg%%n" />
  </formats>
  </seelog>
`

	var config string
	if testing.Verbose() {
		config = fmt.Sprintf(appConfig, "trace")
	} else {
		config = fmt.Sprintf(appConfig, "info")
	}

	logger, err := log.LoggerFromConfigAsBytes([]byte(config))

	if err != nil {
		fmt.Println(err)
		return
	}

	log.ReplaceLogger(logger)
}
Exemple #11
0
func loadAppConfig() {
	appConfig := `
<seelog minlevel="info">
    <outputs formatid="common">
        <rollingfile type="size" filename="/U8SOFT/turbocrm70/apache/logs/ws/roll.log" maxsize="100000" maxrolls="5"/>
        <filter levels="critical">
            <file path="/U8SOFT/turbocrm70/apache/logs/ws/critical.log" formatid="critical"/>
        </filter>
		<filter levels="error">
            <file path="/U8SOFT/turbocrm70/apache/logs/ws/error.log" formatid="error"/>
        </filter>
    </outputs>
    <formats>
        <format id="common" format="%Date/%Time [%LEV] %Msg%n" />
        <format id="critical" format="%File %FullPath %Func %Msg%n" />
		<format id="error" format="%Date/%Time %File %FullPath %Func %Msg%n" />
    </formats>
</seelog>`
	logger, err := seelog.LoggerFromConfigAsBytes([]byte(appConfig))
	if err != nil {
		fmt.Println(err)
		return
	}
	UseLogger(logger)
}
Exemple #12
0
func loadAppConfig() {
	appConfig := `
<seelog type="adaptive" mininterval="2000000" maxinterval="100000000" critmsgcount="500" minlevel="debug">
    <exceptions>
        <exception filepattern="test*" minlevel="error"/>
    </exceptions>
    <outputs formatid="all">
        <file path="all.log"/>
        <filter levels="info">
          <console formatid="fmtinfo"/>
        </filter>
        <filter levels="error,critical" formatid="fmterror">
          <console/>
          <file path="errors.log"/>
        </filter>
    </outputs>
    <formats>
        <format id="fmtinfo" format="[%Level] [%Time] %Msg%n"/>
        <format id="fmterror" format="[%LEVEL] [%Time] [%FuncShort @ %File.%Line] %Msg%n"/>
        <format id="all" format="[%Level] [%Time] [@ %File.%Line] %Msg%n"/>
        <format id="criticalemail" format="Critical error on our server!\n    %Time %Date %RelFile %Func %Msg \nSent by Seelog"/>
    </formats>
</seelog>
`
	logger, err := seelog.LoggerFromConfigAsBytes([]byte(appConfig))
	if err != nil {
		fmt.Println(err)
		return
	}
	UseLogger(logger)
}
func loadAppConf() {
	// appConf := `<seelog />`
	appConf := `
<seelog minlevel="warn">
    <outputs formatid="common">
        <rollingfile type="size" filename="/Users/winniewang/source/go-workspace/logs/roll.log" maxsize="100000" maxrolls="5"/>
        <filter levels="critical">
            <file path="/Users/winniewang/source/go-workspace/logs/critical.log" formatid="critical"/>
            <smtp formatid="criticalemail" senderaddress="*****@*****.**" sendername="Zhou Zhefu" hostname="smtp.gmail.com" hostport="587" username="******" password="******">
                <recipient address="*****@*****.**"/>
            </smtp>
        </filter>
    </outputs>
    <formats>
        <format id="common" format="%Date/%Time [%LEV] %Msg%n" />
        <format id="critical" format="%File %FullPath %Func %Msg%n" />
        <format id="criticalemail" format="Critical error on our server!\n    %Time %Date %RelFile %Func %Msg \nSent by Seelog"/>
    </formats>
</seelog>
	`

	logger, err := seelog.LoggerFromConfigAsBytes([]byte(appConf))
	if err != nil {
		fmt.Println(err)
		return
	}

	UseLogger(logger)
}
Exemple #14
0
func createLogger(logName string) (seelog.LoggerInterface, error) {
	conf, err := createLogConf(logName)
	if err != nil {
		fmt.Println("build log conf err", err)
		os.Exit(1)
	}
	return seelog.LoggerFromConfigAsBytes(conf)
}
Exemple #15
0
func setLogLevel(level string) {
	if level != "error" && level != "warn" {
		log.Infof("Setting log level '%s'.", level)
	}
	logConfig := fmt.Sprintf("<seelog minlevel='%s'>", level)
	logger, _ := log.LoggerFromConfigAsBytes([]byte(logConfig))
	log.ReplaceLogger(logger)
}
Exemple #16
0
// initLoggerFromBytes initializes the logger using the specified configuration as bytes.
func initLoggerFromBytes(seelogConfig []byte) (logger T) {
	var seelogger seelog.LoggerInterface
	var err error
	if seelogger, err = seelog.LoggerFromConfigAsBytes(seelogConfig); err != nil {
		fmt.Println("Error parsing logger config:", err)
		return nil
	}
	seelogDefault = seelogger
	return withContext(seelogDefault)
}
//Creates a new DefaultLogger that is configured to write messages to console with minimum log level Level.
func NewDefaultLogger(Level LogLevel) *DefaultLogger {
	var config = fmt.Sprintf(`<seelog minlevel="%s">
    <outputs formatid="main">
        <console />
    </outputs>

    <formats>
        <format id="main" format="%%Date/%%Time [%%LEVEL] %%Msg%%n"/>
    </formats>
</seelog>`, Level)
	logger, _ := log.LoggerFromConfigAsBytes([]byte(config))
	return &DefaultLogger{Level, logger}
}
func rollingFileWriterManyRolls() {
	testConfig := `
<seelog>
	<outputs>
		<rollingfile type="size" filename="./log/manyrolls.log" maxsize="100" maxrolls="4" />
	</outputs>
</seelog>
`
	logger, _ := log.LoggerFromConfigAsBytes([]byte(testConfig))
	log.ReplaceLogger(logger)
	fmt.Println("Rolling file writer. Many rolls")

	doLogBig()
}
Exemple #19
0
func LoadLogConfig(name string) {
	config := `<seelog type="adaptive" mininterval="2000000" maxinterval="10000000" critmsgcount="5">
				<outputs formatid="msg">
				<file path="` + name + `.log"/>
				</outputs>
				<formats>
				<format id="msg" format="%Time: %Msg%n"/>
				</formats>
				</seelog>`

	logger, _ := log.LoggerFromConfigAsBytes([]byte(config))

	log.ReplaceLogger(logger)
}
Exemple #20
0
func stdFormat() {
	testConfig := `
<seelog type="sync">
	<outputs formatid="main">
		<console/>
	</outputs>
	<formats>
		<format id="main" format=" %Date %Time - [%LEVEL] - %Msg - (%Func %File)%n"/>
	</formats>
</seelog>`

	logger, _ := log.LoggerFromConfigAsBytes([]byte(testConfig))
	log.ReplaceLogger(logger)
}
func consoleWriter() {
	testConfig := `
<seelog>
	<outputs>
		<console />
	</outputs>
</seelog>
`
	logger, _ := log.LoggerFromConfigAsBytes([]byte(testConfig))
	log.ReplaceLogger(logger)
	fmt.Println("Console writer")

	doLog()
}
Exemple #22
0
func defaultFormat() {
	fmt.Println("Default format")

	testConfig := `
<seelog type="sync" />`

	logger, err := log.LoggerFromConfigAsBytes([]byte(testConfig))
	if err != nil {
		fmt.Println(err)
	}
	log.ReplaceLogger(logger)

	log.Trace("Test message!")
}
func loadAdaptiveConfig() {
	testConfig := `<seelog type="adaptive" mininterval="200000000" maxinterval="1000000000" critmsgcount="5">
	<outputs formatid="msg">
		<console/>
	</outputs>
	<formats>
		<format id="msg" format="%Time: %Msg%n"/>
	</formats>
</seelog>`

	logger, _ := log.LoggerFromConfigAsBytes([]byte(testConfig))

	log.ReplaceLogger(logger)
}
func main() {
	defer log.Flush()
	logConfig := `
<seelog>
	<outputs formatid="format1">
		<rollingfile type="size" filename="./log/roll.log" maxsize="100" maxrolls="5" />
	</outputs>
	<formats>
		<format id="format1" format="%Date/%Time [%LEV] %Msg%n"/>
	</formats>
</seelog>
`
	logger, err := log.LoggerFromConfigAsBytes([]byte(logConfig))
	check(err)
	log.ReplaceLogger(logger)
	weatherStation.UseLogger(logger)
	defer weatherStation.FlushLog()

	connection, err := weatherStation.Connect()
	check(err)
	weatherStation.WakeUp(connection)
	mqttClient, opts, err := StartMqttConnection()
	check(err)
	for {
		var wg sync.WaitGroup
		var dataChannel chan *weatherStation.WeatherData = make(chan *weatherStation.WeatherData)
		wg.Add(1)
		go weatherStation.GetCurrentData(connection, 30, dataChannel, &wg)
		go PostCurrentData(dataChannel, mqttClient, opts)
		wg.Wait()
		mqttClient.Disconnect(100)
		time.Sleep(time.Second * 1)
		var connected bool = false
		var i int = 1
		for connected == false {
			mqttClient, _, err = StartMqttConnection()
			if err == nil {
				connected = true
			} else {
				time.Sleep(time.Second * time.Duration(i))
				log.Warnf("wait for %d second to try to reconnect mqtt broker", i)
				i++
			}
		}

		//time.Sleep(time.Second * 5)
	}

	log.Info("END")
}
func smtpWriter() {
	testConfig := `
 <seelog>
  <outputs>
   <smtp senderaddress="*****@*****.**" sendername="Automatic notification service" hostname="mail.none.org" hostport="587" username="******" password="******">
    <recipient address="*****@*****.**"/>
   </smtp>
  </outputs>
 </seelog>
 `
	logger, _ := log.LoggerFromConfigAsBytes([]byte(testConfig))
	log.ReplaceLogger(logger)
	fmt.Println("SMTP writer is now sending emails to the specified recipients")
	doLog()
}
func fileWriter() {

	testConfig := `
<seelog>
	<outputs>
		<file path="./log/log.log"/>
	</outputs>
</seelog>
`
	logger, _ := log.LoggerFromConfigAsBytes([]byte(testConfig))
	log.ReplaceLogger(logger)
	fmt.Println("File writer")

	doLog()
}
func splitDispatcher() {
	testConfig := `
<seelog>
	<outputs>
		<file path="./log/split.log"/>
		<console />
	</outputs>
</seelog>
`
	logger, _ := log.LoggerFromConfigAsBytes([]byte(testConfig))
	log.ReplaceLogger(logger)
	fmt.Println("Split dispatcher")

	doLog()
}
Exemple #28
0
func init() {
	var seelogger seelog.LoggerInterface
	var err error

	fmt.Fprintln(os.Stderr, "Use default config init Logger")
	seelogger, err = seelog.LoggerFromConfigAsBytes([]byte(defaultConfig))

	if err != nil {
		log.Panicf("Can't init Logger,error:%s", err)
		return
	} else {
		//确保取得正确的调用堆栈
		seelogger.SetAdditionalStackDepth(2)
	}
	logger = &SeeLogLogger{seelogger}
}
Exemple #29
0
func InitLogging(level string) error {
	config := fmt.Sprintf(`<seelog minlevel="%s">
    <outputs formatid="main">
        <console />
    </outputs>

    <formats>
        <format id="main" format="%%Date/%%Time [%%LEVEL] %%Msg%%n"/>
    </formats>
</seelog>`, level)

	logger, err := log.LoggerFromConfigAsBytes([]byte(config))
	Logger = logger

	return err
}
func levelsFormatter() {
	testConfig := `
<seelog type="sync">
	<outputs formatid="main">
		<console/>
	</outputs>
	<formats>
		<format id="main" format="%MyLevel %Msg%n"/>
	</formats>
</seelog>`

	logger, _ := log.LoggerFromConfigAsBytes([]byte(testConfig))
	log.ReplaceLogger(logger)

	log.Trace("Test message!")
}