Giter VIP home page Giter VIP logo

logrotate's People

Contributors

easycz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

logrotate's Issues

The filename, directory name, or volume label syntax is incorrect.

Sample code, running on Microsoft Windows, which panics:

package main

import (
	"bufio"
	"fmt"
	"os"
	"path/filepath"
	"time"

	"github.com/easyCZ/logrotate"
	jww "github.com/spf13/jwalterweatherman"
)

func main() {
	jww.SetLogThreshold(jww.LevelTrace)
	jww.SetStdoutThreshold(jww.LevelTrace)

	writer, err := logrotate.New(jww.TRACE, logrotate.Options{
		// Where should the writer be outputting files?
		// If the directory does not exist, it will be created.
		// Required.
		Directory: getExecutableDir(),
		// What is the maximum size of each file?
		// Optional. Use 0 for unlimited.
		MaximumFileSize: 1024 * 1024 * 1024,
		// How often should a new file be created, based on time?
		// Optional. Use 0 to disable time based log rotation.
		MaximumLifetime: time.Hour,
		// How would you like to name your files?
		// Invoked each time a new file is being created.
		FileNameFunc: logrotate.DefaultFilenameFunc,
	})
	if err != nil {
		fatal(err)
	}
	jww.SetLogOutput(writer)

	jww.INFO.Println("This is a test info log.")
	jww.TRACE.Println("This is a test trace log.")
	jww.ERROR.Println("This is a test error log.")
	jww.FATAL.Println("This is a test fatal log.")

	// Ensure all messages are flushed to files before exiting
	err = writer.Close()
	if err != nil {
		fatal(err)
	}
}

func fatal(err error) {
	jww.FATAL.Println(err)
	fmt.Print("Press the Enter key to continue...")
	bufio.NewReader(os.Stdin).ReadBytes('\n')
	os.Exit(1)
}

func getExecutableDir() string {
	ex, err := os.Executable()
	if err != nil {
		jww.FATAL.Println(err)
		panic("Failed to find the executable directory!")
	}
	dir := filepath.Dir(ex)
	return dir
}

Though, on the playground, it works.

On Microsoft Windows it fails with:

INFO 2021/07/22 16:23:43 This is a test info log.
TRACE 2021/07/22 16:23:43 This is a test trace log.
ERROR 2021/07/22 16:23:43 This is a test error log.
FATAL 2021/07/22 16:23:43 This is a test fatal log.
TRACE 2021/07/22 16:23:43 Failed to create log file failed to create new file at D:\2021-07-22T14:23:43Z-spD.log: open D:\2021-07-22T14:23:43Z-spD.log: The filename, directory name, or volume label syntax is incorrect.
TRACE 2021/07/22 16:23:43 Failed to rotate log file failed to create new file at D:\2021-07-22T14:23:43Z-dry.log: open D:\2021-07-22T14:23:43Z-dry.log: The filename, directory name, or volume label syntax is incorrect.
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xc0000005 code=0x0 addr=0x18 pc=0xebbd79]

goroutine 6 [running]:
bufio.(*Writer).Available(...)
        C:/Program Files/Go/src/bufio/bufio.go:624
bufio.(*Writer).Write(0x0, 0xc000010200, 0x32, 0x40, 0xe10, 0x0, 0x1)
        C:/Program Files/Go/src/bufio/bufio.go:634 +0x79
github.com/easyCZ/logrotate.(*Writer).listen(0xc0000a6000)
        D:/SOME/PATH/vendor/github.com/easyCZ/logrotate/writer.go:139 +0x13b
created by github.com/easyCZ/logrotate.New
        D:/SOME/PATH/vendor/github.com/easyCZ/logrotate/writer.go:206 +0x199

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.