duanhui1869 2015-12-10 14:21
浏览 39

Golang从包中导入方法

Now i decided to use leveled logger in my Go programm. At this moment it's logrus. But after some investigation (through pain) found that there are much loggers: glog, log15, logrus and so on. So, now i want try to use logrus and have
possibility to change it on another without fixing all my sources where log is used.

I tried to create my own package logger which only imports logrus:

package logger
import "github.com/Sirupsen/logrus"

func init() {
    logrus.SetFormater(new(logrus.TextFormater))    
}

In all other my sources a want to use i in such way: // main.go

package main
import log "logger"

func main() {
   log.Print(...)
   log.Debug(...)
   and so on
}

But compiler says me that there are no such methods in logger package. Is there a way just import some package A to my own package B and use in my code all methods of A through B?

The reason what i need this is to have a chance quickly change logrus to ahother logger in future which support same methods just replacing several lines of code in my own logger file without replacing import log "github.com/Sirupsen/logrus" on import log "github.com/some_bestlogger" over all sources

  • 写回答

3条回答 默认 最新

  • doulaopu2343 2015-12-10 14:25
    关注

    You can import your logger package simply for its side effects, and still use the logrus package directly:

    package main
    
    import (
        log "github.com/Sirupsen/logrus"
        _ "logger"
    )
    
    func main() {
        log.Print()
        log.Debug()
    }
    

    If you do want to be able to call those log function directly through your "logger" package, you will need to define the functions and delegate them to the "logrus" package internally.

    评论

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行