尝试创建一个包装标准 Logger 类型的类型,但具有所需的增强功能。然后,通过创建一个名为“log”的实例来包装默认记录器,您可以继续以相同的方式在代码中使用日志记录,并且所需的更改最少(因为它将与日志包具有相同的名称,并保留*所有方法)。package mainimport _log "log"type WrappedLogger struct { // This field has no name, so we retain all the Logger methods *_log.Logger}// here we override the behaviour of log.Fatalfunc (l *WrappedLogger) Fatal(v ...interface{}) { l.Println("doing the HTTP request") /// do HTTP request // now call the original Fatal method from the underlying logger l.Logger.Fatal(v...)}// wrapping the default logger, but adding our new method.var log = WrappedLogger{_log.Default()}func main() { // notice we can still use Println log.Println("hello") // but now Fatal does the special behaviour log.Fatal("fatal log")}*这里唯一的问题是,我们用日志实例替换了典型的日志包。在许多方面,它的行为是相同的,因为为了方便起见,日志包中的大多数函数都设置为转发到默认的 Logger 实例。但是,这意味着我们的新成员将无法访问日志包中的“true”函数,例如 log。新建。为此,您需要将别名引用到原始包。log// want to create a new logger?_log.New(out, prefix, flag)