コード例 #1
0
ファイル: FileLogWriter_Manual.go プロジェクト: v-gu/mysqltc
func main() {
	// Get a new logger instance
	log := l4g.NewLogger()

	// Create a default logger that is logging messages of FINE or higher
	log.AddFilter("file", l4g.FINE, l4g.NewFileLogWriter(filename, false))
	log.Close()

	/* Can also specify manually via the following: (these are the defaults) */
	flw := l4g.NewFileLogWriter(filename, false)
	flw.SetFormat("[%D %T] [%L] (%S) %M")
	flw.SetRotate(false)
	flw.SetRotateSize(0)
	flw.SetRotateLines(0)
	flw.SetRotateDaily(false)
	log.AddFilter("file", l4g.FINE, flw)

	// Log some experimental messages
	log.Finest("Everything is created now (notice that I will not be printing to the file)")
	log.Info("The time is now: %s", time.LocalTime().Format("15:04:05 MST 2006/01/02"))
	log.Critical("Time to close out!")

	// Close the log
	log.Close()

	// Print what was logged to the file (yes, I know I'm skipping error checking)
	fd, _ := os.Open(filename)
	in := bufio.NewReader(fd)
	fmt.Print("Messages logged to file were: (line numbers not included)\n")
	for lineno := 1; ; lineno++ {
		line, err := in.ReadString('\n')
		if err == os.EOF {
			break
		}
		fmt.Printf("%3d:\t%s", lineno, line)
	}
	fd.Close()

	// Remove the file so it's not lying around
	os.Remove(filename)
}
コード例 #2
0
ファイル: hello_test.go プロジェクト: QBui/hello_test
func (s *S) TestLog4goModule(c *C) {
	// Setup a log file
	log.AddFilter("file", log.DEBUG, log.NewFileLogWriter("test.log", false))

	// Formatted logging can be done at any of the logging levels (Finest, Fine, Debug, Trace, Info, Warning, Error, Critical)
	log.Trace("Received message: %s (%d)", "Testing", 10)

	// Warnings, Errors, and Criticals provide an os.Error that you can use for a return
	//return l4g.Error("Unable to open file: %s", err)

	// The wrapper functions can also behave like Sprint if the first argument isn't a string
	portno := 1000
	clientid := "123456"
	client := "qbui"
	log.Debug(portno, clientid, client)

	// Use a closure so that if DEBUG isn't logged, it doesn't take any time
	// log.Debug(func()string{ decodeRaw(raw) })
}