Coder Social home page Coder Social logo

phachon / go-logger Goto Github PK

View Code? Open in Web Editor NEW
182.0 17.0 59.0 97 KB

一个简单而强大的 golang 日志工具包,支持同步和异步输出到 命令行,文件, api 接口,文件支持按文件大小,文件行数,日期切分;A simple and powerful golang logging toolkit that supports synchronous and asynchronous output to the console, file, API interfaces, file support by file size, file line number, date sharding.

License: MIT License

Go 100.00%
go logger log golang filelogger console console-log console-color php-logger javascript

go-logger's Introduction

Hey 👋🏽, I'm phachon!

Hi, I'm phachon, a golang/phper/js Developer 🚀 from China, currently, I'm a development engineer.

GIF

Talking about Personal Stuffs:

  • 👨🏽‍💻 I’m currently working on 后厂村;
  • 🌱 I’m currently learning vue and react;
  • 🤔 I’m looking for help with javascript;
  • 💬 Ask me about anything, I am happy to help;
  • 📫 How to reach me: [email protected];

Languages and Tools:
Python Go PHP Tensorflow JavaScript TypeScript
HTML5 CSS3 Bootstrap React Vue Nodejs Docker MySQL

Git GitLab GitHub

Phachon's github stats

Visitor count

⭐️ Show some ❤️ by starring some of the repositories!

From phachon

go-logger's People

Contributors

phachon avatar

Stargazers

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

Watchers

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

go-logger's Issues

您好,我能不能在这里随便写点啥

我是个初学者,想学习下大佬的代码,主要是想琢磨琢磨设计思路啥,能不能在这自说自话写写学习笔记,肯定只写关于go-logger的,谢谢大佬(o゜▽゜)o☆

日志文件设置最大行数并按照year切割后,日志会输出到之前按照日期切割出的文件中

fileConfig := &go_logger.FileConfig{ LevelFileName: map[int]string{ Logger.LoggerLevel("error"): fmt.Sprintf("%s/file.log", logDir), Logger.LoggerLevel("info"): fmt.Sprintf("%s/file.log", logDir), Logger.LoggerLevel("debug"): fmt.Sprintf("%s/file.log", logDir), }, MaxSize: 0, MaxLine: 1000000, DateSlice: "y", JsonFormat: false, Format: logFormat, }
设置如上, 发现一个奇怪的问题4月9号的日志输出到了切割出的4月2号的文件中。

有没有日志自动清除的功能?

想请教下有没有日志自动清除的功能?比如设置日志文件最大个数,超了就循环清?比如日志保存时间,自动清两天前的日志?

打印日志没有代码行数

项目中日志相关配置代码如下

package util

import (
	"fmt"
	goLogger "github.com/phachon/go-logger"
	"os"
)

var Logger *goLogger.Logger

func InitLogger(env, hostname string) error {
	Logger = goLogger.NewLogger()
	_ = Logger.Detach("console")

	if env == "prod" {
		if exists, _ := PathExists(Config.Log.Path); !exists {
			err := os.MkdirAll(Config.Log.Path, os.ModePerm)
			if err != nil {
				return err
			}
		}
		// 文件输出配置
		fileConfig := &goLogger.FileConfig{
			Filename: fmt.Sprintf("%s/aiops-cartoon-gateway-%s.log", Config.Log.Path, hostname), // 日志输出文件名,不自动存在
			// 如果要将单独的日志分离为文件,请配置LealFrimeNem参数。
			LevelFileName: map[int]string{
				Logger.LoggerLevel("error"): fmt.Sprintf("%s/error-%s.log", Config.Log.Path, hostname), // Error 级别日志被写入 error .log 文件
			},
			MaxSize:    1024 * 1024,       // 文件最大值(KB),默认值0不限
			MaxLine:    100000,            // 文件最大行数,默认 0 不限制
			DateSlice:  "d",               // 文件根据日期切分, 支持 "Y" (年), "m" (月), "d" (日), "H" (时), 默认 "no", 不切分
			JsonFormat: Config.Log.IsJson, // 写入文件的数据是否 json 格式化
			Format:     "",                // 如果写入文件的数据不 json 格式化,自定义日志格式
		}
		// 添加 file 为 Logger 的一个输出
		_ = Logger.Attach("file", goLogger.LOGGER_LEVEL_DEBUG, fileConfig)
	} else {
		// 命令行输出配置
		consoleConfig := &goLogger.ConsoleConfig{
			Color:      true,              // 命令行输出字符串是否显示颜色
			JsonFormat: Config.Log.IsJson, // 命令行输出字符串是否格式化
			Format:     "",                // 如果输出的不是 json 字符串,JsonFormat: false, 自定义输出的格式
		}
		// 添加 console 为 Logger 的一个输出
		_ = Logger.Attach("console", goLogger.LOGGER_LEVEL_DEBUG, consoleConfig)
	}
	return nil
}

使用代码如下:

util.Logger.Info("test")

日志结果如下:

2022-04-08 15:21:50.162 [Info] test

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.