Git Product home page Git Product logo

lumberjack's Introduction

The simplified and slightly modified version of Lumberjack package that integrates nicely with the Zap sugarred logging which has a 4-10x faster than the other structured logging.

Both Zap and Lumberjack simplified by the ITRLog package, so please use this ITRLog package instead.

Installation

go get -u github.com/itrepablik/lumberjack

Modified Version of Lumberjack

The main reason why we have some modified version of the Lumberjack package because of this line #222 from the source code of Lumberjack. This function openNew will be executed during the log rotation which renamed the existing backup log file that exceeds the MaxSize in megabytes.

In our use case, we have constantly rotating backup scheduler program that keeps logging every time, this issue occurs as the file can't be renamed because it's open and use by another program error and that's a strange issue that even the existing close file function of Lumberjack can't close the existing used log file and then rename it.

At this point, we decided to clone the Lumberjack and modify it and replace the existing os.Renamed calls at this line #222:

// Original os.Rename at line #222
if err := os.Rename(name, newname); err != nil {
	return fmt.Errorf("can't rename log file: %s", err)
}

and replace it with:

// Copy the current log file instead of just renaming it.
logDir := filepath.Dir(name)
dst := filepath.FromSlash(filepath.Join(logDir, filepath.Base(newname)))
if err := copyFile(name, dst, logDir); err != nil {
	return fmt.Errorf("can't backup the current log file: %s", err)
}

Besides, the copyFile function:

// copyFile copy a single file from the source to the destination.
func copyFile(src, dst, bareDst string) error {
	var err error
	var srcfd *os.File
	var dstfd *os.File
	var srcinfo os.FileInfo

	if srcfd, err = os.Open(src); err != nil {
		fmt.Println(err)
		return err
	}
	defer srcfd.Close()

	os.MkdirAll(bareDst, os.ModePerm) // Create the dst folder if not exist

	if dstfd, err = os.Create(dst); err != nil {
		fmt.Println(err)
		return err
	}
	defer dstfd.Close()

	if _, err = io.Copy(dstfd, srcfd); err != nil {
		fmt.Println(err)
		return err
	}

	if srcinfo, err = os.Stat(src); err != nil {
		fmt.Println(err)
		return err
	}
	return os.Chmod(dst, srcinfo.Mode())
}

License

Code is distributed under MIT license, feel free to use it in your proprietary projects as well.

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.