Postgresql的csv日志設置


PG的日志系統比較完善,除去系統啟動時指定的日志,wal日志等外,下面主要介紹另一個詳細的輸出日志:csv log. 涉及到的參數文件:$PGDATA/postgresql.conf
涉及的主要參數:

log_destination = 'csvlog'
logging_collector = on
log_directory = '/home/postgres/pg_log'
log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log'

1.說明
要開啟csv日志,需要設置logging_collector=on,否則輸不出來。設置完了后需要 重啟DB。
log_destination的默認參數是stderr,不修改的話只輸出標准錯誤信息
log_directory是輸出日志的路徑
log_filename是輸出日志的名稱,采用默認.
還有其他參數具體可參考postgresql.conf中的where to log 部分。

2.查看:

[postgres@localhost pg_log]$ ll /home/postgres/pg_log/
total 4
-rw-------. 1 postgres postgres 2476 Jun 15 15:40 postgresql-2012-06-15_152431.csv
-rw-------. 1 postgres postgres    0 Jun 15 15:24 postgresql-2012-06-15_152431.log

[postgres@localhost pg_log]$ more postgresql-2012-06-15_152431.csv 
2012-06-15 15:24:31.258 CST,,,1882,,4fdae32f.75a,1,,2012-06-15 15:24:31 CST,,0,LOG,00000,"database system was shut down at 2012-06-1
5 15:24:19 CST",,,,,,,,,""
2012-06-15 15:24:31.259 CST,,,1882,,4fdae32f.75a,2,,2012-06-15 15:24:31 CST,,0,LOG,00000,"could not remove cache file ""base/16384/p
g_internal.init"": Not a directory",,,,,,,,,""
2012-06-15 15:24:31.282 CST,,,1885,,4fdae32f.75d,1,,2012-06-15 15:24:31 CST,,0,LOG,00000,"autovacuum launcher started",,,,,,,,,""
2012-06-15 15:24:31.306 CST,,,1880,,4fdae32e.758,1,,2012-06-15 15:24:30 CST,,0,LOG,00000,"database system is ready to accept connect
ions",,,,,,,,,""
.....

3.日志入庫
也就是將文本文件導入到數據庫中,采用copy方式比較簡單。建表:

CREATE TABLE pg_log
(
  log_time timestamp(3) with time zone,
  user_name text,
  database_name text,
  process_id integer,
  connection_from text,
  session_id text,
  session_line_num bigint,
  command_tag text,
  session_start_time timestamp with time zone,
  virtual_transaction_id text,
  transaction_id bigint,
  error_severity text,
  sql_state_code text,
  message text,
  detail text,
  hint text,
  internal_query text,
  internal_query_pos integer,
  context text,
  query text,
  query_pos integer,
  location text,
  application_name text,
  PRIMARY KEY (session_id, session_line_num)
);

NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "pg_log_pkey" for table "pg_log"
CREATE TABLE;

copy

copy pg_log from '/home/postgres/pg_log/postgresql-2012-06-15_152431.csv' with csv;
COPY 4

4.日志的關閉
把開啟日志的過程做反向操作,設置相關值off即可

5.其他
該日志信息可以結合啟動時的輸出日志來查看系統的一個歷史運行情況,是排查系統err有極方便的工具


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM