Using CSV-Format Log Output

Including csvlog in the log_destination list provides a convenient way to import log files into a database table. This option emits log lines in comma-separated-values (CSV) format, with these columns: time stamp with milliseconds, user name, database name, process ID, client host:port number, session ID, per-session line number, command tag, session start time, virtual transaction ID, regular transaction ID, error severity, SQLSTATE code, error message, error message detail, hint, internal query that led to the error (if any), character count of the error position therein, error context, user query that led to the error (if any and enabled by log_min_error_statement), character count of the error position therein, location of the error in the PostgreSQL source code (if log_error_verbosity is set to verbose), and application name. Here is a sample table definition for storing CSV-format log output:

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)
);

To import a log file into this table, use the COPY FROM command:

COPY postgres_log FROM ‘/full/path/to/logfile.csv‘ WITH csv;

There are a few things you need to do to simplify importing CSV log files:

  1. Set log_filename and log_rotation_age to provide a consistent, predictable naming scheme for your log files. This lets you predict what the file name will be and know when an individual log file is complete and therefore ready to be imported.
  2. Set log_rotation_size to 0 to disable size-based log rotation, as it makes the log file name difficult to predict.
  3. Set log_truncate_on_rotation to on so that old log data isn‘t mixed with the new in the same file.
  4. The table definition above includes a primary key specification. This is useful to protect against accidentally importing the same information twice. The COPY command commits all of the data it imports at one time, so any error will cause the entire import to fail. If you import a partial log file and later import the file again when it is complete, the primary key violation will cause the import to fail. Wait until the log is complete and closed before importing. This procedure will also protect against accidentally importing a partial line that hasn‘t been completely written, which would also cause COPY to fail.

logging_collector (boolean)

This parameter enables the logging collector, which is a background process that captures log messages sent to stderr and redirects them into log files. This approach is often more useful than logging to syslog, since some types of messages might not appear in syslog output. (One common example is dynamic-linker failure messages; another is error messages produced by scripts such as archive_command.) This parameter can only be set at server start.

Note: It is possible to log to stderr without using the logging collector; the log messages will just go to wherever the server‘s stderr is directed. However, that method is only suitable for low log volumes, since it provides no convenient way to rotate log files. Also, on some platforms not using the logging collector can result in lost or garbled log output, because multiple processes writing concurrently to the same log file can overwrite each other‘s output.

Note: The logging collector is designed to never lose messages. This means that in case of extremely high load, server processes could be blocked while trying to send additional log messages when the collector has fallen behind. In contrast, syslog prefers to drop messages if it cannot write them, which means it may fail to log some messages in such cases but it will not block the rest of the system.

参考:

http://www.postgresql.org/docs/current/interactive/runtime-config-logging.html

时间: 2024-11-02 11:27:07

Using CSV-Format Log Output的相关文章

sas,log,output,ods输出管理

1:改变log输出到指定外部文件 log一般输出在log窗口,使用printto过程可以改变其默认输出位置 proc printto log = "d:\log.txt" new; *将log输出到指定的文件中,new表示每次覆盖上一次,更多信息到时候查看帮助文档; proc print data=sashelp.class; proc printto; run; *恢复默认log输出; 2:改变output输出到指定外部文件??? proc printto print='e:\log

[Javascript] Grouping and Nesting Console Log Output

Organize your log output by grouping your logs into collapsable hierarchies using console.group(). for(var i = 0; i < 100; i++){ var num = Math.random() * 100; console.groupCollapsed("Picking a random number!"); console.log("Number great

[转]Learn SQLite in 1 hour

转载说明: 1.原文地址:http://www.askyb.com/sqlite/learn-sqlite-in-1-hour/ 2.译文地址:http://www.oschina.net/question/12_53183(红薯翻译) 3.英文原文有5处错误,下面的已经修正过了 原文如下: Learn SQLite in 1 hour askyb on May, 9th 2012 in SQLite 1. Introduction SQLite is an open source, embed

EF6 SQL Logging – Part 2: Changing the content/formatting | One Unicorn

In part 1 we saw how DbContext.Database.Log can be used to easily log SQL to the console or some other TextWriter. This post covers how the context and formatting of the output can be changed. DatabaseLogFormatter Under the covers the Database.Log pr

Logging and Intercepting Database Operations (EF6 Onwards)

Logging and Intercepting Database Operations Starting with Entity Framework 6, anytime Entity Framework sends a command to the database this command can be intercepted by application code. This is most commonly used for logging SQL, but can also be u

做U3d编辑器xlsx批量生成csv和python批量转

做U3d编辑器xlsx批量生成csvxlsx编辑就是用强大的office Excel 2013 ,  也就是说 不管是程序还是美术只需要维护这个Excel就够了,然后一键批量转化为csv文件即可! 打包不会打包Excel,游戏中也不用这个!然后来到Unity3d的编辑器菜单.在Unity的Editor文件夹下面需要这四个文件: 参考了:http://www.codeproject.com/Articles/246772/Convert-xlsx-xls-to-csv 的window窗口程序. 游

mongodb-导出数据到csv文件或json文件

在mongodb的bin目录下, 有一个mongoexport, 可用于数据的导出 [[email protected] bin]$ ./mongoexport --help Usage: mongoexport <options> Export data from MongoDB in CSV or JSON format. See http://docs.mongodb.org/manual/reference/program/mongoexport/ for more informati

C++、Java、JavaScript中的日志(log)

编程思想之日志记录 什么是log? 相信你一定用日记写过点滴心事,或是用空间.微信.微博刷着动态,记录你每天的喜怒哀乐!在程序中也有一种类似的东西,记录着他主人(应用程序)每天的行踪,他叫日志(log).日记--是人类生活的记事本,日志(log)--是程序运行状况的记事本. 顾名思义,日志(log,后面均以log称之)就是用来记录程序每天的运行状况的,比如程序出现异常的情况,或是某个关键点,功某个重要的数据或交易等.这里的每天不是说每天一记,可以是伴随着程序运行的始终,只要程序在运行着就一直在记

[Practical Git] Filter commit history with git log arguments

In the last lesson, we learned how to format the git log output; in this lesson we will learn how to filter down to a specific set of commits. By default, git log shows every commit in a repo. We will walk through using a bunch of options to filter o