首页 > 其他分享 >Go - Change What Is Being Logged by the Standard Logger

Go - Change What Is Being Logged by the Standard Logger

时间:2023-09-29 15:11:44浏览次数:38  
标签:What log Being fields Logged time line event happened

Problem: You want to change what the standard logger logs.


Solution: Use the SetFlags function to set flags and add fields to each log line.

 

The default behavior of the standard logger adds the date and time fields to each line of the log. 

The log package allows you to add information along with the default date and time fields. You can add these fields using the SetFlag function. The fields that are provided include:

Date
• The date in local time zone
Time
• The time in local time zone
Microseconds
• The microsecond resolution of the time field
UTC
• Use UTC time zone instead of local time zone if date or time fields are set
Long file
• The full filename and line number
Short file
• The filename and the line number
Message prefix position
• Move the prefix (from SetPrefix ) from the beginning of the line to before the start of the message


Here are some examples. You start by setting only the date field in the log:

log . SetFlags ( log . Ldate )
log . Println ( "Some event happened" )

This produces:

2022/01/24 Some event happened


If you want to add the time with microsecond details, you do this:

log . SetFlags ( log . Ldate | log . Lmicroseconds )
log . Println ( "Some event happened" )

Using the or operator on the flags, you set up the various fields to use with the log. Here’s the result from before:

2022/01/24 20:43:54.595365 Some event happened

 

The file fields are interesting because you can use them to tell you where the problems lie in the code through the logs:

log . SetFlags ( log . Ldate | log . Lshortfile )
log . Println ( "Some event happened" )

It gives you additional information about the filename and the line where the

problem occurred:
2022/01/24 20:51:02 logging.go:20: Some event happened

 

标签:What,log,Being,fields,Logged,time,line,event,happened
From: https://www.cnblogs.com/zhangzhihui/p/17737006.html

相关文章

  • What is Lambda?
    根据我的观察,Lambda是一种比较灵活的形式,需要多看几个案例才能明白它。Lambda是一种简化代码的技术手段,主要用于简化匿名实现类,允许把函数作为一个方法的参数传递进方法中。它本身并不会创造出新的概念和功能来。不过现在很多开源项目都使用到这种技术,我的原则是能看懂别人代码即......
  • What is Serializable ?
    在Java中,Serializable是一个标记接口(markerinterface),用于指示一个类的对象可以被序列化。序列化是将对象转换为字节流的过程,可以将对象保存到文件、在网络上传输或在内存中传递。当一个类实现了Serializable接口时,它表示该类的对象可以被序列化和反序列化。序列化过程通过将对......
  • 8.2 BeingDebugged
    BeingDebugged是Windows系统PEB结构体中的一个成员,它是一个标志位,用于标识当前进程是否正在被调试。BeingDebugged的值为0表示当前进程未被调试,值为1表示当前进程正在被调试。由于BeingDebugged是在PEB结构体中存储的,因此可以通过访问PEB结构体来获取BeingDebugged的值。恶意软件......
  • Microservice - What are microservices, and why are microservices?
    Theconceptof microservicesissimplybreakingasinglelargepotentialserviceintomanysmaller servicesthatworktogether,hence,thename.Oneveryobviousadvantagewhenitcomestobuildinganapplicationwitha microservicearchitecturewouldbe......
  • What is service discovery?
    Servicediscoveryhelpsyoudiscovery,trackandmonitorthehealthofserviceswithinanetwork.Servicediscoveryregistersandmaintainsarecordofallyourservicesinaservicecatalog.Thisservicecatalogactsasasinglesourceoftruththatallows......
  • What's the difference between Industrial Maxon Wireless 802.11ac AP and router
    ThemaindifferencebetweenindustrialAPsandroutersistheirintendeduse.IndustrialAPsaredesignedforuseinharshenvironments,suchasfactories,warehouses,andoutdoorlocations.Theyaretypicallymoreruggedandhaveawidertemperaturerang......
  • 闪电WhatsApp云控
    WhatsApp作为全球使用率最高的应用程序之一,跟我们国内的微信、QQ类似,都是用来与用户进行交谈交友的平台,在全球180多个国家或地区深受欢迎,月活跃用户量是非常庞大的。做海外营销,你肯定是离不开WhatsApp的。不知道大家有没有听过WhatsApp云控?WhatsApp云控是第三方公司开......
  • Vue编译出现This file is being treated as an ES module because it has a '.js' fil
    问题描述在编译前端项目时出现下面的问题:FailedtoloadPostCSSconfig:FailedtoloadPostCSSconfig(searchPath:D:/WebProject/imooc-front):[FailedtoloadPostCSSconfig]FailedtoloadPostCSSconfig(searchPath:D:/WebProject/imooc-front):Thisfileisbe......
  • Breaking Changes When Upgrading from EF Core 6 to 7: What You Need to Know
    EntityFrameworkCore(EFCore)isapopularObject-RelationalMapping(ORM)frameworkusedby.NETdevelopersfordatabaseoperations.WiththereleaseofEFCore7,manydevelopersareconsideringupgradingtheirprojectstotakeadvantageofthenewfe......
  • Seeing What You Said: Talking Face Generation Guided by a Lip Reading Expert 论
    最近一直在看虚拟人像. 最关键的论文就是wav2lip.目前项目中也是用的这个.一个视频加一个语音,就可以生成用视频里面的头,加语音的新视频.现在看这篇论文SeeingWhatYouSaid:TalkingFaceGenerationGuidedbyaLipReadingExpert.主要是搜了没有相关论文,所以就自己......