在golang中将日志内容写入自定义文件

huangapple go评论84阅读模式
英文:

Writing log content to custom file in golang

问题

我运行一个服务器,将日志内容重定向到一个文件,比如说"current.log",用于调试目的,但我遇到了一个场景,需要向用户展示一组特定的日志,以便他们可以跟踪后台正在进行的过程。所以我尝试为每个用户编写另一组日志(每个用户一个日志文件),我需要向用户展示这些日志,但这些日志不包含任何安全数据。

英文:

I run a server which redirects the log content to a file, say "current.log" for debugging purpose which I cannot show to the user. But I got a scenario where I need to show a specific set of logs to the user so that they can follow up the process going on in the back end. So I tried writing another set of logs(in a custom file) for each user(one log file per user) which i need to show to the user which does not contain any secure data.

答案1

得分: 2

一种解决方法是使用日志记录机制,您可以为日志记录器创建一个对象(具有特定的文件位置),然后使用该对象来写入日志。这样,使用特定对象写入的日志将被重定向到该特定文件。

> 我使用的日志记录包是 "github.com/sadlil/gologger"

示例代码:

package main
import (
     "github.com/sadlil/gologger"
)

func main() {
    logger := gologger.GetLogger(gologger.FILE, "/home/user/path/user.log")
    logger.Log("Test file log")
}

> 注意:文件将在对象创建时自动创建

因此,您可以为每个用户动态创建一个日志记录器对象(每个用户都有一个不同的日志文件),并将日志重定向到该文件。日志的格式将为
> [LOG] [2016-04-07 11:31:28] [main::test.go::main] [8] Test file log

英文:

One way to solve this is to use logger mechanism where you can create an object for your logger(with specific file location) and then write logs using that object. So that, the logs written using the specific object will be redirected to that particular file.

> The logger package that I used is "github.com/sadlil/gologger"

Sample code:

package main
import (
     "github.com/sadlil/gologger"
)

func main() {
    logger := gologger.GetLogger(gologger.FILE, "/home/user/path/user.log")
    logger.Log("Test file log")
}

> Note: file will be automatically created at the time of object creation

so you can create a logger object for each user dynamically(each user gets a different log file) and logs wil be redirected to that file.
The logging format will be
> [LOG] [2016-04-07 11:31:28] [main::test.go::main] [8] Test file log

答案2

得分: 2

你可以使用lumberjack.v2,通过它你可以为每个用户定义一个自定义的日志文件。

在下面的代码片段中,我使用一个简单的布尔值来确定是否将日志内容添加到每个用户的日志文件中。

package main

import (
	"gopkg.in/natefinch/lumberjack.v2"
	"io/ioutil"
	"log"
	"strconv"
)

type user struct {
	id     int
	logger *log.Logger
}

func createUser(id int, logWanted bool) user {
	var l *log.Logger

	if logWanted {
		// 这里的日志内容将会被添加到用户的日志文件中
		userFile := &lumberjack.Logger{
			Filename:   "user_log_" + strconv.Itoa(id) + ".log",
			MaxSize:    250, // mb
			MaxBackups: 5,
			MaxAge:     10, // in days
		}
		l = log.New(userFile, "User: ", log.Ldate|log.Ltime|log.Lshortfile)
	} else {
		// 这里的日志内容将不会被记录
		l = log.New(ioutil.Discard, "User: ", log.Ldate|log.Ltime|log.Lshortfile)
	}
	return user{id, l}
}

func doSomething(u user) {
	u.logger.Printf("Log content: user id %v \n", u.id)
}

func main() {
	user1 := createUser(1, true)
	user2 := createUser(2, false)
	user3 := createUser(3, true)
	doSomething(user1)
	doSomething(user2)
	doSomething(user3)
}

这将为每个用户创建一组滚动日志文件,并将日志打开。

  • "user_log_1.log" 是给 user1 的日志文件
  • "user_log_3.log" 是给 user3 的日志文件
  • user2 没有日志文件。
英文:

You can use lumberjack.v2, with it you can define a custom log file for each user.

In the following snippet I use a simple boolean to determine if the log content should be added to the log file or not for each user.

package main

import (
    "gopkg.in/natefinch/lumberjack.v2"
	"io/ioutil"
	"log"
	"strconv"
)

type user struct {
	id     int
	logger *log.Logger
}

func createUser(id int, logWanted bool) user {
	var l *log.Logger

	if logWanted {
		// Here the log content will be added in the user log file
		userFIle := &lumberjack.Logger{
			Filename:   "user_log_" + strconv.Itoa(id) + ".log",
			MaxSize:    250, // mb
			MaxBackups: 5,
			MaxAge:     10, // in days
		}
		l = log.New(userFIle, "User: ", log.Ldate|log.Ltime|log.Lshortfile)
	} else {
		// Here the log content will go nowhere
		l = log.New(ioutil.Discard, "User: ", log.Ldate|log.Ltime|log.Lshortfile)
	}
	return user{id, l}
}

func doSomething(u user) {
	u.logger.Printf("Log content: user id %v \n", u.id)
}

func main() {
	user1 := createUser(1, true)
	user2 := createUser(2, false)
	user3 := createUser(3, true)
	doSomething(user1)
	doSomething(user2)
	doSomething(user3)
}

This will create a new set of rolling log files for each user with the log turned on.

  • "user_log_1.log" for the user1
  • "user_log_3.log" for the user3
  • and no log file for the user2

huangapple
  • 本文由 发表于 2016年4月7日 15:05:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/36468993.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定