postgres日志配置方式

     postgre安装完之后,默认情况下会包含pg_xlog(包含WAL文件的子目录)、pg_clog(事务提交日志,包含事务提交状态数据的子目录),并没有pg_log目录,需要手动创建该目录。

$PGDATA/postgresql.conf中关于log的配置参数如下

#------------------------------------------------------------------------------
# ERROR REPORTING AND LOGGING
#------------------------------------------------------------------------------

# - Where to Log -

#log_destination = 'stderr'             # Valid values are combinations of
                                        # stderr, csvlog, syslog, and eventlog,
                                        # depending on platform.  csvlog
                                        # requires logging_collector to be on.

# This is used when logging to stderr:
#logging_collector = off                # Enable capturing of stderr and csvlog
                                        # into log files. Required to be on for
                                        # csvlogs.
                                        # (change requires restart)

# These are only used if logging_collector is on:
#log_directory = 'pg_log'               # directory where log files are written,
                                        # can be absolute or relative to PGDATA
#log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log'        # log file name pattern,
                                        # can include strftime() escapes
#log_file_mode = 0600                   # creation mode for log files,
                                        # begin with 0 to use octal notation
#log_truncate_on_rotation = off         # If on, an existing log file with the
                                        # same name as the new log file will be
                                        # truncated rather than appended to.
                                        # But such truncation only occurs on
                                        # time-driven rotation, not on restarts
                                        # or size-driven rotation.  Default is
                                        # off, meaning append to existing files
                                        # in all cases.
#log_rotation_age = 1d                  # Automatic rotation of logfiles will
                                        # happen after that time.  0 disables.
#log_rotation_size = 10MB               # Automatic rotation of logfiles will
                                        # happen after that much log output.
                                        # 0 disables.

# These are relevant when logging to syslog:
#syslog_facility = 'LOCAL0'
#syslog_ident = 'postgres'
#syslog_sequence_numbers = on
#syslog_split_messages = on

# This is only relevant when logging to eventlog (win32):
# (change requires restart)
#event_source = 'PostgreSQL'

# - When to Log -

#client_min_messages = notice           # values in order of decreasing detail:
                                        #   debug5
                                        #   debug4
                                        #   debug3
                                        #   debug2
                                        #   debug1
                                        #   log
                                        #   notice
                                        #   warning
                                        #   error

#log_min_messages = warning             # values in order of decreasing detail:
                                        #   debug5
                                        #   debug4
                                        #   debug3
                                        #   debug2
                                        #   debug1
                                        #   info
                                        #   notice
                                        #   warning
                                        #   error
                                        #   log
                                        #   fatal
                                        #   panic

#log_min_error_statement = error        # values in order of decreasing detail:
                                        #   debug5
                                        #   debug4
                                        #   debug3
                                        #   debug2
                                        #   debug1
                                        #   info
                                        #   notice
                                        #   warning
                                        #   error
                                        #   log
                                        #   fatal
                                        #   panic (effectively off)

#log_min_duration_statement = -1        # -1 is disabled, 0 logs all statements
                                        # and their durations, > 0 logs only
                                        # statements running at least this number
                                        # of milliseconds


# - What to Log -

#debug_print_parse = off
#debug_print_rewritten = off
#debug_print_plan = off
#debug_pretty_print = on
#log_checkpoints = off
#log_connections = off
#log_disconnections = off
#log_duration = off
#log_error_verbosity = default          # terse, default, or verbose messages
#log_hostname = off
#log_line_prefix = ''                   # special values:
                                        #   %a = application name
                                        #   %u = user name
                                        #   %d = database name
                                        #   %r = remote host and port
                                        #   %h = remote host
                                        #   %p = process ID
                                        #   %t = timestamp without milliseconds
                                        #   %m = timestamp with milliseconds
                                        #   %n = timestamp with milliseconds (as a Unix epoch)
                                        #   %i = command tag
                                        #   %e = SQL state
                                        #   %c = session ID
                                        #   %l = session line number
                                        #   %s = session start timestamp
                                        #   %v = virtual transaction ID
                                        #   %x = transaction ID (0 if none)
                                        #   %q = stop here in non-session
                                        #        processes
                                        #   %% = '%'
                                        # e.g. '<%u%%%d> '
#log_lock_waits = off                   # log lock waits >= deadlock_timeout
#log_statement = 'none'                 # none, ddl, mod, all
#log_replication_commands = off
#log_temp_files = -1                    # log temporary files equal or larger
                                        # than the specified size in kilobytes;
                                        # -1 disables, 0 logs all temp files
log_timezone = 'Etc/GMT0'


# - Process Title -

#cluster_name = ''                      # added to process titles if nonempty
                                        # (change requires restart)
#update_process_title = on

<======默认情况下log相关的参数值

#log_destination = 'stderr' 
#logging_collector = off
#log_directory = 'pg_log'               # directory where log files are written,
#log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log'        # log file name pattern,
#log_file_mode = 0600                   # creation mode for log files,
#log_truncate_on_rotation = off  
#log_rotation_age = 1d                  # Automatic rotation of logfiles will
#log_rotation_size = 10MB
#log_min_messages = warning  

    输出格式默认为标准错误输出,遇到的错误直接显示出来。日志收集默认是关闭的,log的目录默认是$PGDATA/pg_log(该目录刚开始是不存在的),如果产生日志格式为'postgresql-%Y-%m-%d_%H%M%S.log'

<=====创建pg_log日志文件夹(确保权限可读可写)

[postgres@qxy data]$ pwd
/spark/pgsql/data
[postgres@qxy data]$ mkdir pg_log

<======修改之后的参数如下,并重新启动postgres

log_destination = 'csvlog'
logging_collector = on
log_directory = 'pg_log'
log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log'
#log_file_mode = 0600   
log_rotation_age = 1d
log_rotation_size = 100MB
log_min_messages = info

[postgres@qxy data]$ 
[postgres@qxy data]$ pg_ctl stop -D /spark/pgsql/data -m fast    <=====关闭postgres
waiting for server to shut down.... done
server stopped
[postgres@qxy data]$ pg_ctl start -D /spark/pgsql/data           <=====重启启动postgres
server starting
[postgres@qxy data]$ LOG:  redirecting log output to logging collector process
HINT:  Future log output will appear in directory "pg_log".     <======以后的日志将要出现在pg_log目录

[postgres@qxy data]$ 
[postgres@qxy data]$ 

<======终端操作产生错误

postgres=# 
postgres=# 
postgres=# \d t1
             Table "public.t1"
 Column |         Type          | Modifiers 
--------+-----------------------+-----------
 name   | character varying(31) | 

postgres=# insert into t1 select md5(random()::text);
ERROR:  value too long for type character varying(31)   <======md5产生的长度大于31字节
postgres=# 
postgres=# 

<=====查看产生的错误文件

postgres@qxy pg_log]$ 
[postgres@qxy pg_log]$ ls -ltr
total 8
-rw-------. 1 postgres postgres   96 Sep 28 07:51 postgresql-2018-09-28_075114.log
-rw-------. 1 postgres postgres 1039 Sep 28 07:53 postgresql-2018-09-28_075114.csv  
[postgres@qxy pg_log]$ 
[postgres@qxy pg_log]$ more postgresql-2018-09-28_075114.csv 
2018-09-28 07:51:14.371 GMT,,,10248,,5baddd72.2808,1,,2018-09-28 07:51:14 GMT,,0,LOG,00000,"ending log output to stderr",,"Future log output will go to log d
estination ""csvlog"".",,,,,,,""
2018-09-28 07:51:14.375 GMT,,,10250,,5baddd72.280a,1,,2018-09-28 07:51:14 GMT,,0,LOG,00000,"database system was shut down at 2018-09-28 07:51:01 GMT",,,,,,,,
,""
2018-09-28 07:51:14.378 GMT,,,10250,,5baddd72.280a,2,,2018-09-28 07:51:14 GMT,,0,LOG,00000,"MultiXact member wraparound protections are now enabled",,,,,,,,,
""
2018-09-28 07:51:14.380 GMT,,,10248,,5baddd72.2808,2,,2018-09-28 07:51:14 GMT,,0,LOG,00000,"database system is ready to accept connections",,,,,,,,,""
2018-09-28 07:51:14.381 GMT,,,10254,,5baddd72.280e,1,,2018-09-28 07:51:14 GMT,,0,LOG,00000,"autovacuum launcher started",,,,,,,,,""
2018-09-28 07:53:20.343 GMT,"postgres","postgres",10270,"[local]",5badddc1.281e,1,"INSERT",2018-09-28 07:52:33 GMT,2/19,0,ERROR,22001,"value too long for typ
e character varying(31)",,,,,,"insert into t1 select md5(random()::text);",,,"psql.bin"
[postgres@qxy pg_log]$ 
[postgres@qxy pg_log]$ 

<=====错误文件插入表中(格式化日志内容)

CREATE TABLE postgres_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)
);


postgres=# CREATE TABLE postgres_log
postgres-# (
postgres(#   log_time timestamp(3) with time zone,
postgres(#   user_name text,
postgres(#   database_name text,
postgres(#   process_id integer,
postgres(#   connection_from text,
postgres(#   session_id text,
postgres(#   session_line_num bigint,
postgres(#   command_tag text,
postgres(#   session_start_time timestamp with time zone,
postgres(#   virtual_transaction_id text,
postgres(#   transaction_id bigint,
postgres(#   error_severity text,
postgres(#   sql_state_code text,
postgres(#   message text,
postgres(#   detail text,
postgres(#   hint text,
postgres(#   internal_query text,
postgres(#   internal_query_pos integer,
postgres(#   context text,
postgres(#   query text,
postgres(#   query_pos integer,
postgres(#   location text,
postgres(#   application_name text,
postgres(#   PRIMARY KEY (session_id, session_line_num)
postgres(# );
CREATE TABLE
postgres=# 
postgres=# 
postgres=# copy postgres_log from '/spark/pgsql/data/pg_log/postgresql-2018-09-28_075114.csv' with csv;
COPY 6
postgres=# 

<=====查询产生的错误

postgres=# select log_time,database_name,user_name,application_name,message from postgres_log;
          log_time          | database_name | user_name | application_name |                         message                          
----------------------------+---------------+-----------+------------------+----------------------------------------------------------
 2018-09-28 07:51:14.371+00 |               |           |                  | ending log output to stderr
 2018-09-28 07:51:14.375+00 |               |           |                  | database system was shut down at 2018-09-28 07:51:01 GMT
 2018-09-28 07:51:14.378+00 |               |           |                  | MultiXact member wraparound protections are now enabled
 2018-09-28 07:51:14.38+00  |               |           |                  | database system is ready to accept connections
 2018-09-28 07:51:14.381+00 |               |           |                  | autovacuum launcher started
 2018-09-28 07:53:20.343+00 | postgres      | postgres  | psql.bin         | value too long for type character varying(31)
 (6 rows)

postgres=# 

 

你可能感兴趣的:(postgres日志配置方式)