http://blog.51cto.com/eric1/1854574

一.增加haproxy日志记录功能

 

1.1 由于数据分析的需要,我们必须打开haproxy日志,记录相关信息。

在配置前,我们先来了解一下日志的level:local0~local7 16~23保留为本地使用

emerg 0 系统不可用
     alert 1 必须马上采取行动的事件
     crit 2 关键的事件
     err 3 错误事件
     warning 4 警告事件
     notice 5 普通但重要的事件
     info 6 有用的信息
     debug 7 调试信息

1.2 实际配置:

########################################

vi haproxy.conf(在default处添加如下信息)
defaults
log global
option httplog
log 127.0.0.1 local6
########################################

rsyslog配置

###

# vim /etc/rsyslog.conf
#由于haproxy的日志是用udp传输的,所以要启用rsyslog的udp监听
# Provides UDP syslog reception
$ModLoad imudp
$UDPServerRun 514

#启用级别为local2的设备,并将该设备的所有级别的日志全部输出到/var/log/haproxy.log下
local6.* /var/log/haproxy.log

###

#######################################

vi /etc/rsyslog.conf(添加如下内容)
local3.* /data/logs/haproxy.log
#######################################

#######################################

vi /etc/sysconfig/rsyslog

把SYSLOGD_OPTIONS="-m 0"
改成 SYSLOGD_OPTIONS="-r -m 0 -c 2"
#######################################

相关解释说明:
-r:打开接受外来日志消息的功能,其监控514 UDP端口;
-x:关闭自动解析对方日志服务器的FQDN信息,这能避免DNS不完整所带来的麻烦;
-m:修改syslog的内部mark消息写入间隔时间(0为关闭),例如240为每隔240分钟写入一次"--MARK--"信息;
-h:默认情况下,syslog不会发送从远端接受过来的消息到其他主机,而使用该选项,则把该开关打开,所有
接受到的信息都可根据syslog.conf中定义的@主机转发过去

配置完毕后重启rsyslogd和haproxy即可.

日志内容如下:

Sep 18 15:48:11 localhost haproxy[12124]: Proxy html-server started.
Sep 18 15:48:11 localhost haproxy[12124]: Proxy html-server started.
Sep 18 15:48:29 localhost haproxy[12125]: 10.69.112.139:59244 [18/Sep/2016:15:48:29.027] http-in html-server/html-A 0/0/0/0/0 200 254 - - --NI 0/0/0/0/0 0/0 "GET / HTTP/1.1" Sep 18 15:48:29 localhost haproxy[12125]: 10.69.112.139:59244 [18/Sep/2016:15:48:29.027] http-in html-server/html-A 0/0/0/0/0 200 254 - - --NI 0/0/0/0/0 0/0 "GET / HTTP/1.1" Sep 18 15:48:29 localhost haproxy[12125]: 10.69.112.139:59244 [18/Sep/2016:15:48:29.027] http-in html-server/html-A 0/0/0/0/0 200 254 - - --NI 0/0/0/0/0 0/0 "GET / HTTP/1.1"

但日志可读性很差,我们需要显示更多的信息,但默认的都捕获不到,只能自定义需要显示的内容了。

二.定义haproxy日志输出内容和格式

2.1自定义haproxy日志。直接上配置文件。

global log 127.0.0.1 local3 info maxconn 4096 chroot /gomeo2o/haproxyuid haproxy gid haproxy daemon nbproc 1 description haproxypidfile /var/run/haproxy.pid
defaults log global
#    option httplogbalance roundrobinmode http maxconn 4096 retries 3 option redispatch option forcecloseoption dontlognulloption abortonclosestats uri /haproxy
frontend http-in bind 0.0.0.0:80 mode http log global option httplog option httpclose acl html url_reg -i \.html$ use_backend html-server if html default_backend html-server #((    capture request header Host len 64capture request header User-Agent len 128capture request header X-Forwarded-For len 100capture request header Referer len 200capture response header Server len 40capture response header Server-ID len 40#capture捕获信息 log-format %ci:%cp\ %si:%sp\ %B\ %U\ %ST\ %r\ %b\ %f\ %bi\ %hrl\ %hsl\ #log-forcat定义日志需显示内容(变量) #利用capture捕获信息,log-forcat定义变量 #详解请见2.1和2.2 #)) backend html-server mode http balance roundrobin option httpchk GET /index.html cookie SERVERID insert indirect nocache server html-A 10.69.112.150:80 weight 1 cookie 3 check inter 2000 rise 2 fall 5 server html-B 10.69.112.148:80 weight 1 cookie 4 check inter 2000 rise 2 fall 5

其他参数在此不做过多解释,主要查看两个中括号之间的内容。

2.2 capture捕获

haproxy利用capture捕获请求(request)和响应(response)信息,具体需捕获内容可以做如下操作:

用浏览器访问一个网站(我用的google),比如www.gomeplus.com,按F12,内容如下(注意我红色框起来的部分):

找到response headers项,这里就是你可以捕获的信息。比如捕获server和server-id:

capture response header Server len 40
capture response header Server-ID len 40

备注:2.1配置文件当中的capture基本的信息都能捕获到了,如有特别需求,请查阅官网(capture到底能捕获多少信息,多详细,我也在查询当中)。

2.3 定义log-format变量

定义示例:

log-format %T\ %t\ Some\ Text
log-format %{+Q}o\ %t\ %s\ %{-Q}r

请参阅下面的表为定义的变量(转自官方文档):

 +---+------+-----------------------------------------------+-------------+| R | var  | field name (8.2.2 and 8.2.3 for description) | type | +---+------+-----------------------------------------------+-------------+ | | %o | special variable, apply flags on all next var | | +---+------+-----------------------------------------------+-------------+ | | %B | bytes_read (from server to client) | numeric | | H | %CC | captured_request_cookie | string | | H | %CS | captured_response_cookie | string | | | %H | hostname | string | | H | %HM | HTTP method (ex: POST) | string | | H | %HP | HTTP request URI without query string (path) | string | | H | %HQ | HTTP request URI query string (ex: ?bar=baz) | string | | H | %HU | HTTP request URI (ex: /foo?bar=baz) | string | | H | %HV | HTTP version (ex: HTTP/1.0) | string | | | %ID | unique-id | string | | | %ST | status_code | numeric | | | %T | gmt_date_time | date | | | %Tc | Tc | numeric | | | %Td | Td = Tt - (Tq + Tw + Tc + Tr) | numeric | | | %Tl | local_date_time | date | | H | %Tq | Tq | numeric | | H | %Tr | Tr | numeric | | | %Ts | timestamp | numeric | | | %Tt | Tt | numeric | | | %Tw | Tw | numeric | | | %U | bytes_uploaded (from client to server) | numeric | | | %ac | actconn | numeric | | | %b | backend_name | string | | | %bc | beconn (backend concurrent connections) | numeric | | | %bi | backend_source_ip (connecting address) | IP | | | %bp | backend_source_port (connecting address) | numeric | | | %bq | backend_queue | numeric | | | %ci | client_ip (accepted address) | IP | | | %cp | client_port (accepted address) | numeric | | | %f | frontend_name | string | | | %fc | feconn (frontend concurrent connections) | numeric | | | %fi | frontend_ip (accepting address) | IP | | | %fp | frontend_port (accepting address) | numeric | | | %ft | frontend_name_transport ('~' suffix for SSL) | string | | | %lc | frontend_log_counter | numeric | | | %hr | captured_request_headers default style | string | | | %hrl | captured_request_headers CLF style | string list | | 

转载于:https://www.cnblogs.com/linkenpark/p/10287447.html

HAproxy增加日志记录功能和自定义日志输出内容、格式相关推荐

  1. Xshell 7设置及使用日志记录功能

    设置日志记录功能 右击已建立的会话,选择底部属性,点击左下角日志记录,进入日志记录功能设置对话窗.一般按照如下图所示设置即可. 使用日志记录功能 菜单栏"文件-日志-启动/停止", ...

  2. 配置Haproxy增加日志记录功能

    2019独角兽企业重金招聘Python工程师标准>>> CentOS 7上yum安装的Haproxy,默认没有记录日志.需要做一下配置才能记录日志. 1.创建日志文件/var/log ...

  3. 如何自行给指定的SAP OData服务添加自定义日志记录功能

    有的时候,SAP标准的OData实现或者相关的工具没有提供我们想记录的日志功能,此时可以利用SAP系统强大的扩展特性,进行自定义日志功能的二次开发. 以SAP CRM Fiori应用"My ...

  4. java aop日志记录_AOP实现日志记录功能

    场景:整个系统的DML操作需要记录日志 记录内容:1 操作人 2 操作时间 3 操作类型(登录 新增 修改 删除) 4 操作描述  5 详细请求数据(varchar2()) 6 操作IP  ==> ...

  5. php实现项目的日志记录功能,tp5框架使用composer实现日志记录功能示例

    本文实例讲述了tp5框架使用composer实现日志记录功能.分享给大家供大家参考,具体如下: tp5实现日志记录 1.安装 psr/log composer require psr/log 它的作用 ...

  6. java core日志在哪里_java-如何在未启用日志记录功能的情况下在...

    我已使用CXF 3.0.0 Milestone1创建了Rest服务,并且试图在SEND阶段的Out Interceptor中获取HTTP响应的正文,并将其放入String变量中,而未在xml配置文件中 ...

  7. java日志怎么实现_JAVA项目中怎么实现一个通用日志记录功能

    JAVA项目中怎么实现一个通用日志记录功能 发布时间:2020-11-21 17:04:50 来源:亿速云 阅读:53 作者:Leah 今天就跟大家聊聊有关JAVA项目中怎么实现一个通用日志记录功能, ...

  8. 文件服务器导出共享文件列表,服务器共享文件访问记录方法、共享文件操作日志记录功能实现...

    现在很多单位都有共享文件服务器,经常会在文件共享服务器上存储单位一些重要的共享文件,让局域网用户访问使用,为了保护共享文件的安全,管理员常常需要设置共享文件访问权限,同时还需要记录共享文件访问日志,如 ...

  9. 按键精灵--日志记录功能和释放附件功能

    日志记录功能实例 logStart "F:\日志.log"//日志记录开始,保存位于F盘 For i=0 to 9TracePrint "i="&i N ...

最新文章

  1. discuz x3插件开发傻瓜图文教程,用demo说话
  2. PI-安装SoapUI on Windows
  3. #模拟触手机屏幕_2部手机经受埃隆马斯克火焰枪炙烤:iPhoneXS最终胜出
  4. struts.properties属性解释
  5. Followme Devops step by step
  6. linux 进程监控命令2——ps
  7. nohup命令让Linux程序永远在后台执行
  8. (116)System Verilog类合成(类包含关系)详解
  9. 大数据之-Hadoop_推荐系统框架图---大数据之hadoop工作笔记0015
  10. 李彦宏开年内部信:2018营收破千亿,做出好产品的百度已归来
  11. linux内核多个补丁,一个令人惊叹的Linux内核补丁
  12. Javascript之把网页加入收藏夹功能
  13. CentOS 6.5 Thinkpad 小红点(TrackPoint)设置
  14. Temporal Action Proposal
  15. AutoCAD C# 自动加载自定义RibbonUI界面
  16. 计算机顶级会议Rankings
  17. 百度信息流介绍,没有比这更详细的啦
  18. 刚刚!中国最有前途的30座城市排名公布
  19. MySQL事件的创建和执行
  20. 图扑软件数字孪生海上风电 | 向海图强,奋楫争先

热门文章

  1. Revit开发读取CAD信息
  2. Localize a WPF application
  3. linux软件管理及软件仓库
  4. 实现个人域名跳转指定网站
  5. 程序员外包到底怎么了?
  6. 利用OpenCV从图片中提取矩形并标注坐标(室内平面地图)——(一)
  7. Android 微信h5支付
  8. MacOS下Go语言环境搭建
  9. uni-app 第三方授权登录
  10. 撸了一次 Js 代码