如何在golang中创建一个包,根据“调用者”程序来使用任何记录器?

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

How to create a package to use any logger, depending on the "caller" program in golang?

问题

根据你的描述,你想创建一个包,该包应该使用调用者的日志记录包。你想知道是否可能实现这样的功能,以及是否有方法可以做到这一点。

在你提供的示例代码中,主要代码使用了logrus包进行日志记录。你想要创建的包是mypack。

根据你的需求,你可以将调用者的日志记录包作为参数传递给mypack的Logger字段。这样,mypack就可以使用调用者的日志记录包进行日志记录。

在mypack代码中的DoSomething函数中,你可以使用Logger字段进行日志记录,例如调用Logger.Debug、Logger.Info和Logger.Error方法。

这样,当你在主代码中调用myPack.DoSomething()时,mypack就可以使用调用者的日志记录包进行日志记录了。

希望这可以帮助到你!如果你有任何其他问题,请随时提问。

英文:

As I can see, each developer has its own need and approach to solve same things/needs. As an example, logging. There are many logging packages that works in different ways and each developer choose the one that best fits their needs/preferences. Thinking on that, I would like to create a package that should use the caller's logging package. Is it possible? Someone have a way to do that?

Something like this:

Main code using logrus package:

package main

import (
	"os"

	"github.com/Sirupsen/logrus"

	"gitlab.com/tutume/_testing/globallogs/mypack"
)

var Log = logrus.New()

var myPack pack.Pack

func main() {
	isDebug := os.Getenv("MYAPP_LOGLEVEL")
	switch isDebug {
	case "Debug":
		Log.Level = logrus.DebugLevel
	case "Info":
		Log.Level = logrus.InfoLevel
	default:
		Log.Level = logrus.ErrorLevel
	}
	Log.Formatter = &logrus.TextFormatter{
		ForceColors: true,
	}

	Log.Debug("Debugging...")
	Log.Info("Informing...")
	Log.Error("Normal...")

	myPack.Logger = Log
	myPack.DoSomething()

}

mypack code:

package pack

type Pack struct {
	Logger interface{}
}

func (mp *Pack) DoSomething() {
	//	Logger.Debug("Debugging...")
	//	Logger.Info("Informing...")
	//	Logger.Error("Normal...")
}

答案1

得分: 2

如果我理解你的意图,你需要定义一个日志记录包要实现的接口。如果你将Logger定义为空接口,你将无法调用任何这些方法。相反,你需要定义一个包含你想要使用的方法(如Debug、Info、Error等)的接口。任何日志记录包都必须实现这些方法。如果某个特定的日志记录包没有正确的方法集,你需要编写一些包装代码来实现你定义的接口。

英文:

If I understand what you're trying to do, you need to define an interface that a logging package would implement. If you define Logger as an empty interface, you won't be able to call any of those methods. Instead, you need to define an interface that contains the methods you want to use (Debug, Info, Error, etc.). Any logging package would then have to implement those methods. If a particular logging package doesn't have the right set of methods already, you would have to write some wrapper code to implement the interface as you define it.

答案2

得分: 0

我继续搜索并找到了这篇帖子。非常有趣。
作为一名基础设施经理/技术人员,我一直觉得很多我使用(和曾经使用过)的软件都缺乏尽可能深入的信息来识别和解决问题,甚至用于记录所有内容以进行审计/安全建议。很多时候,即使在调试模式下,我们也无法找到问题的确切原因,最后只能找到“可能”的原因。现在重新回到开发世界,看到开发人员如何使用日志和库,我想这可能是一个原因(主程序与其库之间缺乏“互操作性”)。由于库通常具有“自己的”日志记录(或根本不记录),而且很多时候无法对库内部发生的情况进行“完全控制”,在大多数情况下,使用这些库开发主程序的开发人员将无法为其应用程序提供必要的日志详细信息。当然,我理解在开源世界中,如果您希望获得更详细的信息(包括您使用的库),由于它是开源的,您可以简单地进行更改,问题就解决了。但是,这可能会使库的使用(主要是为了节省时间而使用库,换句话说是可重用性)不如应该的那样有帮助。
正如引用的帖子所提到的,处理这个问题的一种方法是始终向调用者发送“必要”的信息以供处理。但通常仅适用于错误、致命错误和可能的信息级别事件。但是对于调试和跟踪等情况,这在开发库时并不“实用”(由于很多原因),因此我认为一个重要原因是当开发人员开发库时,他已经需要所有的调试和跟踪来进行测试,但不一定需要发送回调用者。帖子中详细介绍的另一种方法是使用处理程序,但通过这样做,库的开发人员将需要做更多的工作。正如安迪所回答的(并且在帖子中也讨论过),另一种选择是创建一个接口,该接口将接收主程序的日志记录器并使用它,为了使此选项正常工作,它应该使用stdlib log(或实现最常见的日志级别的某个日志库),以防调用程序不关心日志记录。
对于我现在的“愿望”(因为我正在重新开始编程,而且在Go方面完全是初学者),我将创建一些“丑陋的代码”来满足我的需求,并继续学习和尝试实现“完美的日志系统”,目前来看,我认为使用接口是安迪建议的最佳选择。

英文:

I kept searching and found this post. Very interesting.
As a infrastructure manager/technician, one thing that I've always missed from many software that I use (and have used) is the ability to have as deeper information as possible to identify and fix issues or even for logging everything for auditing/security proposal. Many times, we face with some issues that even in debug mode we aren't able to find the exactly cause of the problem, we ended up just finding the "probable" cause. Now getting back into development world, and seeing how developer are working with logs and libraries, I'm thinking that this could be one reason (the lack of "interoperability between the main programs and its libraries). As libraries usually have "their own" logging (or not loggin at all) and many times, doesn't give a "full control" on what is happening inside the library, in most cases the developer who is developing the main program (using such libraries) will not be able to give its own application the necessary logging details. Of course, I understand that in the open source world, if you want to have a more detailed information including from libraries that you use, as it is open source, you can simple make your changes and it is done. But then, this could make the use of a library (thinking in the advantage of using libraries mainly for not spending time to "recreate the wheel", in other world reusability) not as helpful as it should be.
As the referenced post mentioned, one way to handle it is by always sending to the caller the "necessary" information to be handled. But usually it works for errors, fatals and maybe info level events. But for debug and trace for instance, it isn't "pratical" when developing libraries (for many reasons), and then I think that one big reason is that when a developer is developing a library, he will already need all debugs and traces for testing, but not necessarily sending back to the caller. Another way detailed in the post is by using handlers, but by doing this, the developer of the library will have much more work to do. As Andy answered (and is discussed in the post too), an alternative is to create an interface that will receive the main's program logger and use it and for this option to work properly, it should use the stdlib log (or some logging library that implement the most comum logging levels) in case the caller program doesn't case about logging.
For my "wishes" right now (as I'm benning in programming again and totally beggining in Go), I will create some "ugly code" to handle my needs and keep learning and trying ways to accomplish the "perfect logging system" which by now, IMHO is using interfaces as Andy suggested.

huangapple
  • 本文由 发表于 2016年3月22日 01:54:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/36138315.html
匿名

发表评论

匿名网友

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

确定