Git Product home page Git Product logo

file-rotator's Introduction

About file-rotator

This is a golang library helps to write content into file and automatically rotate the file. It implements io.Writer and io.Closer, so you can combine it with golang standard log library, or others log library such as logrus.

These are it's features:

  • Rotate according to file size
  • Rotate according to file lines
  • Rotate daily
  • Remove files that are n days ago

Usage

This is a example shows how to create a file rotator and combine with the logrus and the golang standard log library.

import (
    log "github.com/Sirupsen/logrus"
    rotator "github.com/firnsan/file-rotator"
    "io"
    stdlog "log"
    "os"
)

func InitLog() error {
    var err error
    level, err := log.ParseLevel(gApp.Cnf.LogLevel)
    if err != nil {
        log.Errorf("Parse log level failed: %s", err)
        return err 
    }   
    // Create a file rotator
    fw, err := rotator.NewFileRotator(gApp.Cnf.LogDir + "/app.log")
    if err != nil {
        log.Errorf("Set log failed: %s", err)
        return err 
    }   

    log.SetOutput(fw)
    log.SetLevel(level)

    // Also need to set the golang standard log's output to this writer                                                                                   
    w := log.StandardLogger().Writer()
    stdlog.SetOutput(w)

    return nil 
}

func UninitLog() {
    // Close Writer
    w := log.StandardLogger().Out
    log.SetOutput(os.Stderr)
    if wc, ok := w.(io.Closer); ok {
        wc.Close()
    }
    log.Printf("Uninit log success")
}


file-rotator's People

Contributors

firnsan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

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.