Пример #1
0
func main() {
	cfg := &logwriter.Config{
		BufferSize:       0,                  // no buffering
		FreezeInterval:   1 * time.Hour,      // freeze log file every hour
		HotMaxSize:       100 * logwriter.MB, // 100 MB max file size
		CompressColdFile: true,               // compress cold file
		HotPath:          "/var/log/mywebserver",
		ColdPath:         "/var/log/mywebserver/arch",
		Mode:             logwriter.ProductionMode, // write to file only
	}

	lw, err := logwriter.NewLogWriter("mywebserver",
		cfg,
		true, // freeze hot file if exists
		nil)

	if err != nil {
		panic(err)
	}

	logger := log.New(lw, "mywebserver", log.Ldate|log.Ltime)
	logger.Println("Module started")

	if err := lw.Close(); err != nil {
		// Error handling
	}

	return
}
Пример #2
0
func BenchmarkLogWriteBuffered(b *testing.B) {

	// because go test -bench runs Benchmark function a few times
	if err := os.Remove("test.log"); err != nil {
		if !os.IsNotExist(err) {
			b.Fatal(err)
		}
	}

	lw, err := logwriter.NewLogWriter("test",
		&logwriter.Config{BufferSize: 1 * 1024 * 1024,
			//BufferFlushInterval: 200 * time.Millisecond,
			HotMaxSize:       4 * 1024 * 1024,
			CompressColdFile: true,
			ColdPath:         "", Mode: logwriter.ProductionMode}, true, nil)

	if err != nil {
		b.Fatal(err)
	}

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		n, err := lw.Write(typicalLogItem)
		if err != nil {
			b.Fatal(err, n)
		}
	}

	if err := lw.Close(); err != nil {
		b.Fatal(err)
	}

	return
}
Пример #3
0
func ExampleNewLogWriter() {

	cfg := &logwriter.Config{
		BufferSize: 2 * logwriter.MB, // write buffering enabled
		HotPath:    "/var/log/example",
		ColdPath:   "/var/log/example/arch",
		Mode:       logwriter.ProductionMode} // write into a file only

	lw, err := logwriter.NewLogWriter("mywebserver",
		cfg,
		true, // freeze log file if it exists
		nil)

	if err != nil {
		panic(err)
	}

	l := log.New(lw, "mywebserver ", log.Ldate|log.Ltime|log.Lmicroseconds)

	l.Println("Mywebserer started at ", time.Now())

	if err := lw.Close(); err != nil {
		panic(err)
	}
}
Пример #4
0
func main() {

	f, err := os.Create("cpu.out")
	if err != nil {
		fmt.Println(err)
		return
	}
	pprof.StartCPUProfile(f)
	defer pprof.StopCPUProfile()

	if err := os.Remove("test.log"); err != nil {
		if !os.IsNotExist(err) {
			fmt.Println(err)
			return
		}
	}

	lw, err := logwriter.NewLogWriter("test",
		&logwriter.Config{BufferSize: 2 * logwriter.MB,
			HotMaxSize:       10 * logwriter.MB,
			CompressColdFile: true,
			ColdPath:         "", Mode: logwriter.ProductionMode}, true, nil)

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

	buf := append(bytes.Repeat([]byte("R"), 256), '\n')
	fmt.Println("Started")
	t := time.Now()
	for i := 0; i < 1000000; i++ {
		n, err := lw.Write(buf)
		if err != nil {
			fmt.Println(err, n)
			break
		}
	}

	fmt.Println("Write duration: ", time.Now().Sub(t))

	if err := lw.Close(); err != nil {
		fmt.Println(err)
	}

	fmt.Println("Full duration: ", time.Now().Sub(t))

	return
}