首页 > 其他分享 >Go - Logging to the System Log Service

Go - Logging to the System Log Service

时间:2023-09-30 09:13:55浏览次数:34  
标签:code Logging log facility System syslog rsyslog Go message

Problem: You want to log in to the system log instead of your logfiles.


Solution: Use the log/syslog package to write to syslog.

 

Syslog is a standard network - based logging protocol. It has long been the de facto standard for logging system events and was created by Eric Allman in the 1980s as part of the Sendmail project. The protocol was documented in RFC 3164 by the Internet Engineering Task Force (IETF). Subsequently, IETF standardized it in RFC 5424.
A syslog message (as in RFC 3164) consists of three parts:

Priority
• Includes the facility and the severity
Header
• Includes the timestamp and the hostname or IP address of the machine
Message
• Includes the tag and the content

The facility describes the type of system that sends the log message. It allows log messages from different facilities to be handled differently. There are 24 facilities defined by the RFCs; here are a few:
• 0 (kernel)
• 1 (user - level)
• 2 (mail)
• 3 (system daemons)
• 4 (security/authorization messages)

The severity level is similar to the log level. Syslog defines eight different levels, with 0 being the highest and 7 being the lowest:
• 0 (Emergency)
• 1 (Alert)
• 2 (Critical)
• 3 (Error)
• 4 (Warning)
• 5 (Notice)
• 6 (Informational)
• 7 (Debug)
The timestamp and the hostname or IP address are self - explanatory. The tag is the name of the program that generated the message, while the content is the details of the log message.

Syslog is not implemented uniformly in different operating systems. A popular implementation of syslog is rsyslog, the default syslog implementation in many Linux variants including Debian, Ubuntu, openSUSE, and others.

Go provides a log/syslog package as part of the standard library to interface with syslog. However, it doesn’t work on all systems. For example, it doesn’t work with Windows because it’s not implemented on Windows.
The example in this recipe is based on running against rsyslog on Ubuntu 20.04, and it should work on systems with rsyslog. However, I have not tried it on all systems and implementations.
Before we start on the Go code, you need to set up rsyslog to show the priority, header, and message parts. In rsyslog this is done using a template in the rsyslog configuration file.
Start by editing the /etc/rsyslog.conf configuration file:

$  sudo  vi  /etc/rsyslog.conf

Add the template configuration after this line — $ActionFileDefaultTemplate RSYSLOG_TraditionalFileFormat in the configuration file:

$template  gosyslogs,"%syslogseverity - text%  %syslogfacility - text%  %hostname% %timegenerated%  %syslogtag%  %msg%\n"
$ActionFileDefaultTemplate  gosyslogs

In this configuration, you name the template gosyslogs . You set it to show the severity first, followed by the facility, then the hostname and the timestamp, and finally, the tag and message.
Once you save this file, restart rsyslog:

sudo  service  rsyslog  restart

Now that you have set up rsyslog, you can look at the code. Sending log messages to syslog using the syslog package is relatively straightforward:

var   logger   * log . Logger 

func   init ()   { 
      var   err   error 
      logger ,   err   =   syslog . NewLogger ( syslog . LOG_USER | syslog . LOG_NOTICE ,   0 ) 
      if   err   !=   nil   { 
         log . Fatal ( "cannot  write  to  syslog:  " ,   err ) 
      } 
} 

func   main ()   { 
      logger . Print ( "hello  syslog!" ) 
}

You use the NewLogger function to create a logger, passing the syslog priority flags you want to set. The syslog package provides flags for the facility and the severity levels. You can or them together to send the facility code and the severity level. For the case of the preceding code, you send in syslog.LOG_USER indicating the user facility code, and syslog.LOG_NOTICE indicating the notice severity level.
Run the code first in the file named main.go :
$ go run main.go
Now check the syslogs. Run this on the command line:

$ sudo tail /var/log/syslog

You should see a bunch of log messages, but somewhere at the bottom, you should see something like this:

notice user myhostname Jan 26 15:30:08 /tmp/go - build2223050573/b001/exe/
main[163995]:
hello syslog!

The first field is notice , the severity level, followed by user , which is the facility code. Next is myhostname , which is the hostname, followed by the timestamp.
The next field is the tag , which is the /tmp/go - build2223050573/b001/exe/main[163995] field in the log message. Why is it indicating that it’s in the /tmp directory? That’s because you’re using go run . It will look different if you compile the code and run the binary file. The final field in the log message is the details of the log, which you print out using logger .

 

标签:code,Logging,log,facility,System,syslog,rsyslog,Go,message
From: https://www.cnblogs.com/zhangzhihui/p/17737593.html

相关文章

  • Go每日一库之147:goldmark(Markdown转html)
    简介使用Markdown书写结构化的文档和评论已经相当流行了,Web服务需要将用户编写的Markdown文本转换为html以便浏览器渲染,还常常需要对Markdown语法进行自定义扩展以实现个性化的功能。本期要介绍的**goldmark**就是Go生态中的一款Markdown解析器和扩展器,与GitHub......
  • 进化算法中的遗传算法(Genetic Algorithms)
    进化算法中的遗传算法(GeneticAlgorithms)引言进化算法是一类基于自然进化原理的优化算法,通过模拟生物进化过程中的选择、交叉和变异等操作,来求解复杂问题。遗传算法(GeneticAlgorithms)是进化算法中最为经典和常用的一种方法。本文将介绍遗传算法的基本原理、核心操作和应用领域,以及......
  • Go每日一库之169:dongle(编解码、加解密)
    一个轻量级、语义化、对开发者友好的golang编码解码、加密解密库。安装使用//使用github库goget-ugithub.com/golang-module/dongleimport("github.com/golang-module/dongle")//使用gitee库goget-ugitee.com/golang-module/dongleimport("g......
  • Go每日一库之168:redsync(redis分布式锁)
    今天给大家推荐的是基于redis的Go版本的分布式锁工具:redsync。该工具也是redis官网上推荐的。redsync基于redis的高可用、高性能、防死锁、防误删的分布式锁实现,具有高性能、高可用、防死锁、防误删的特点。一、分布式锁基础知识什么是分布式锁锁,在编程语言中就是一个变量,该变......
  • Go每日一库之167:emoji(emoji表情)
    大家在使用微信或钉钉聊天时,一定使用过表情符号。今天就给大家介绍一个能够在终端上显示emoji表情符号的包:emoji。实现原理:emoji表情符号实际上就是在unicode编码表中有定义的一个编码。通过将符号的文字表示和对应的unicode编码进行一一对应,在使用时对文字符号进行替换成rune字......
  • Go每日一库之187:singleflight(合并重复调用)
    本文主要介绍Go语言中的singleflight包,包括什么是singleflight以及如何使用singleflight合并请求解决缓存击穿问题。singleflight目前(Go1.20)还属于Go的准标准库,它提供了重复函数调用抑制机制,使用它可以避免同时进行相同的函数调用。第一个调用未完成时后续的重复调用会等待,当第......
  • Go每日一库之186:sonic(高性能JSON库)
    介绍我们在日常开发中,常常会对JSON进行序列化和反序列化。Golang提供了encoding/json包对JSON进行Marshal/Unmarshal操作。但是在大规模数据场景下,该包的性能和开销确实会有点不够看。在生产环境下,JSON序列化和反序列化会被频繁的使用到。在测试中,CPU使用率接近10%,其中极端情况......
  • Go每日一库之184:katana(新一代爬虫框架)
    项目链接https://github.com/projectdiscovery/katana项目简介katana是一个使用golang编写的新一代爬虫框架,支持HTTP和headless抓取网页信息不仅可以作为库集成到Golang项目,还可以通过命令行直接抓取,对于有一些轻量级的抓取任务的开发者配合jq一起使用简直就是福......
  • Go每日一库之183:vegeta(http压力测试工具库)
    项目地址:https://github.com/tsenart/vegetahttps://mp.weixin.qq.com/s/J0PiqTifr_rs_S2CzMRoWg......
  • Go每日一库之182:RuleGo(轻量级高性能嵌入式规则引擎)
    ◆ 一、开源项目简介RuleGo是一个基于Go语言的轻量级、高性能、嵌入式的规则引擎。也一个灵活配置和高度定制化的事件处理框架。可以对输入消息进行过滤、转换、丰富和执行各种动作。◆ 二、开源协议使用Apache-2.0开源协议◆ 三、界面展示规则链规则链是规则节点及其关......