Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit3bf66d6

Browse files
committed
Extend the format of CSV logs to include the additional information supplied
with the logged event. CSV logs are now a first-class citizen along plaintext logs in that they carry much of the same information.Per complaint from depesz on bug #3799.
1 parent22867ab commit3bf66d6

File tree

2 files changed

+100
-78
lines changed

2 files changed

+100
-78
lines changed

‎doc/src/sgml/config.sgml

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- $PostgreSQL: pgsql/doc/src/sgml/config.sgml,v 1.158 2007/11/28 15:42:30 petere Exp $ -->
1+
<!-- $PostgreSQL: pgsql/doc/src/sgml/config.sgml,v 1.159 2007/12/11 15:19:05 alvherre Exp $ -->
22

33
<chapter Id="runtime-config">
44
<title>Server Configuration</title>
@@ -3169,7 +3169,13 @@ local0.* /var/log/postgresql
31693169
with these columns: timestamp with milliseconds, user name, database
31703170
name, session ID, host:port number, process ID, per-process line
31713171
number, command tag, session start time, virtual transaction ID,
3172-
regular transaction id, error severity, SQL state code, error message.
3172+
regular transaction id, error severity, SQL state code, error message,
3173+
error message detail, hint, internal query that led to the error (if
3174+
any), character count of the error position thereof, error context,
3175+
user query that led to the error (if any and enabled by
3176+
<varname>log_min_error_statement</>), character count of the error
3177+
position thereof, location of the error in the PostgreSQL source code
3178+
(if <varname>log_error_verbosity</> is set to <literal>verbose</>).
31733179
Here is a sample table definition for storing CSV-format log output:
31743180

31753181
<programlisting>
@@ -3189,6 +3195,14 @@ CREATE TABLE postgres_log
31893195
error_severity text,
31903196
sql_state_code text,
31913197
message text,
3198+
detail text,
3199+
hint text,
3200+
internal_query text,
3201+
internal_query_pos integer,
3202+
context text,
3203+
query text,
3204+
query_pos integer,
3205+
location text,
31923206
PRIMARY KEY (session_id, process_line_num)
31933207
);
31943208
</programlisting>

‎src/backend/utils/error/elog.c

Lines changed: 84 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
*
4343
*
4444
* IDENTIFICATION
45-
* $PostgreSQL: pgsql/src/backend/utils/error/elog.c,v 1.198 2007/11/15 21:14:40 momjian Exp $
45+
* $PostgreSQL: pgsql/src/backend/utils/error/elog.c,v 1.199 2007/12/11 15:19:05 alvherre Exp $
4646
*
4747
*-------------------------------------------------------------------------
4848
*/
@@ -134,7 +134,6 @@ static const char *error_severity(int elevel);
134134
staticvoidappend_with_tabs(StringInfobuf,constchar*str);
135135
staticboolis_log_level_output(intelevel,intlog_min_level);
136136
staticvoidwrite_pipe_chunks(char*data,intlen,intdest);
137-
staticvoidget_csv_error_message(StringInfobuf,ErrorData*edata);
138137
staticvoidwrite_csvlog(ErrorData*edata);
139138

140139
/*
@@ -1612,18 +1611,21 @@ log_line_prefix(StringInfo buf)
16121611
}
16131612
}
16141613

1615-
16161614
/*
16171615
* append a CSV'd version of a string to a StringInfo
16181616
* We use the PostgreSQL defaults for CSV, i.e. quote = escape = '"'
1617+
* If it's NULL, append nothing.
16191618
*/
1620-
16211619
staticinlinevoid
16221620
appendCSVLiteral(StringInfobuf,constchar*data)
16231621
{
16241622
constchar*p=data;
16251623
charc;
16261624

1625+
/* avoid confusing an empty string with NULL */
1626+
if (p==NULL)
1627+
return;
1628+
16271629
appendStringInfoCharMacro(buf,'"');
16281630
while ((c=*p++)!='\0')
16291631
{
@@ -1635,15 +1637,14 @@ appendCSVLiteral(StringInfo buf, const char *data)
16351637
}
16361638

16371639
/*
1638-
* Constructs the error message, depending on the Errordata it gets,
1639-
* in CSV (comma separated values) format. The COPY command
1640-
* can then be used to load the messages into a table.
1640+
* Constructs the error message, depending on the Errordata it gets, in a CSV
1641+
* format which is described in doc/src/sgml/config.sgml.
16411642
*/
16421643
staticvoid
16431644
write_csvlog(ErrorData*edata)
16441645
{
1645-
StringInfoDatamsgbuf;
16461646
StringInfoDatabuf;
1647+
boolprint_stmt= false;
16471648

16481649
/* static counter for line numbers */
16491650
staticlonglog_line_number=0;
@@ -1664,24 +1665,15 @@ write_csvlog(ErrorData *edata)
16641665
}
16651666
log_line_number++;
16661667

1667-
initStringInfo(&msgbuf);
16681668
initStringInfo(&buf);
16691669

1670-
/*
1671-
* The format of the log output in CSV format: timestamp with
1672-
* milliseconds, username, databasename, session id, host and port number,
1673-
* process id, process line number, command tag, session start time,
1674-
* virtual transaction id, regular transaction id, error severity, sql
1675-
* state code, error message.
1676-
*/
1677-
1678-
/* timestamp_with_milliseconds */
16791670

16801671
/*
1681-
* Check if the timestamp is already calculated for the syslog message, if
1682-
* it is, then no need to calculate it again, will use the same, else get
1683-
* the current timestamp. This is done to put same timestamp in both
1684-
* syslog and csvlog messages.
1672+
* timestamp with milliseconds
1673+
*
1674+
* Check if the timestamp is already calculated for the syslog message,
1675+
* and use it if so. Otherwise, get the current timestamp. This is done
1676+
* to put same timestamp in both syslog and csvlog messages.
16851677
*/
16861678
if (formatted_log_time[0]=='\0')
16871679
{
@@ -1715,38 +1707,26 @@ write_csvlog(ErrorData *edata)
17151707

17161708
/* username */
17171709
if (MyProcPort)
1718-
{
1719-
constchar*username=MyProcPort->user_name;
1720-
1721-
if (username==NULL||*username=='\0')
1722-
username=_("[unknown]");
1723-
1724-
appendCSVLiteral(&buf,username);
1725-
}
1710+
appendCSVLiteral(&buf,MyProcPort->user_name);
17261711
appendStringInfoChar(&buf,',');
17271712

1728-
/*databasename */
1713+
/*database name */
17291714
if (MyProcPort)
1730-
{
1731-
constchar*dbname=MyProcPort->database_name;
1732-
1733-
if (dbname==NULL||*dbname=='\0')
1734-
dbname=_("[unknown]");
1735-
1736-
appendCSVLiteral(&buf,dbname);
1737-
}
1715+
appendCSVLiteral(&buf,MyProcPort->database_name);
17381716
appendStringInfoChar(&buf,',');
17391717

17401718
/* session id */
17411719
appendStringInfo(&buf,"%lx.%x", (long)MyStartTime,MyProcPid);
17421720
appendStringInfoChar(&buf,',');
17431721

1744-
/* Remote host and port(is it safe to not quote this?)*/
1722+
/* Remote host and port */
17451723
if (MyProcPort&&MyProcPort->remote_host)
17461724
{
1725+
appendStringInfoChar(&buf,'"');
17471726
appendStringInfo(&buf,"%s",MyProcPort->remote_host);
17481727
if (MyProcPort->remote_port&&MyProcPort->remote_port[0]!='\0')
17491728
appendStringInfo(&buf,":%s",MyProcPort->remote_port);
1729+
appendStringInfoChar(&buf,'"');
17501730
}
17511731
appendStringInfoChar(&buf,',');
17521732

@@ -1762,13 +1742,17 @@ write_csvlog(ErrorData *edata)
17621742
/* PS display */
17631743
if (MyProcPort)
17641744
{
1745+
StringInfoDatamsgbuf;
17651746
constchar*psdisp;
17661747
intdisplen;
17671748

1749+
initStringInfo(&msgbuf);
1750+
17681751
psdisp=get_ps_display(&displen);
17691752
appendStringInfo(&msgbuf,"%.*s",displen,psdisp);
17701753
appendCSVLiteral(&buf,msgbuf.data);
1771-
resetStringInfo(&msgbuf);
1754+
1755+
pfree(msgbuf.data);
17721756
}
17731757
appendStringInfoChar(&buf,',');
17741758

@@ -1787,7 +1771,7 @@ write_csvlog(ErrorData *edata)
17871771

17881772
/* Virtual transaction id */
17891773
/* keep VXID format in sync with lockfuncs.c */
1790-
if (MyProc!=NULL)
1774+
if (MyProc!=NULL&&MyProc->backendId!=InvalidBackendId)
17911775
appendStringInfo(&buf,"%d/%u",MyProc->backendId,MyProc->lxid);
17921776
appendStringInfoChar(&buf,',');
17931777

@@ -1796,53 +1780,77 @@ write_csvlog(ErrorData *edata)
17961780
appendStringInfoChar(&buf,',');
17971781

17981782
/* Error severity */
1799-
appendStringInfo(&buf,"%s,",error_severity(edata->elevel));
1783+
appendStringInfo(&buf,"%s",error_severity(edata->elevel));
1784+
appendStringInfoChar(&buf,',');
18001785

18011786
/* SQL state code */
1802-
appendStringInfo(&buf,"%s,",unpack_sql_state(edata->sqlerrcode));
1787+
appendStringInfo(&buf,"%s",unpack_sql_state(edata->sqlerrcode));
1788+
appendStringInfoChar(&buf,',');
18031789

1804-
/* Error message and cursor position if any */
1805-
get_csv_error_message(&buf,edata);
1790+
/* errmessage */
1791+
appendCSVLiteral(&buf,edata->message);
1792+
appendStringInfoCharMacro(&buf,',');
18061793

1807-
appendStringInfoChar(&buf,'\n');
1794+
/* errdetail */
1795+
appendCSVLiteral(&buf,edata->detail);
1796+
appendStringInfoCharMacro(&buf,',');
18081797

1809-
/* If in the syslogger process, try to write messages direct to file */
1810-
if (am_syslogger)
1811-
write_syslogger_file(buf.data,buf.len,LOG_DESTINATION_CSVLOG);
1812-
else
1813-
write_pipe_chunks(buf.data,buf.len,LOG_DESTINATION_CSVLOG);
1798+
/* errhint */
1799+
appendCSVLiteral(&buf,edata->hint);
1800+
appendStringInfoCharMacro(&buf,',');
18141801

1815-
pfree(msgbuf.data);
1816-
pfree(buf.data);
1817-
}
1802+
/* internal query */
1803+
appendCSVLiteral(&buf,edata->internalquery);
1804+
appendStringInfoCharMacro(&buf,',');
18181805

1819-
/*
1820-
* Appends the buffer with the error message and the cursor position, all
1821-
* CSV escaped.
1822-
*/
1823-
staticvoid
1824-
get_csv_error_message(StringInfobuf,ErrorData*edata)
1825-
{
1826-
char*msg=edata->message ?edata->message :_("missing error text");
1827-
charc;
1806+
/* if printed internal query, print internal pos too */
1807+
if (edata->internalpos>0&&edata->internalquery!=NULL)
1808+
appendStringInfo(&buf,"%d",edata->internalpos);
1809+
appendStringInfoCharMacro(&buf,',');
18281810

1829-
appendStringInfoCharMacro(buf,'"');
1811+
/* errcontext */
1812+
appendCSVLiteral(&buf,edata->context);
1813+
appendStringInfoCharMacro(&buf,',');
18301814

1831-
while ((c=*msg++)!='\0')
1815+
/* user query --- only reported if not disabled by the caller */
1816+
if (is_log_level_output(edata->elevel,log_min_error_statement)&&
1817+
debug_query_string!=NULL&&
1818+
!edata->hide_stmt)
1819+
print_stmt= true;
1820+
if (print_stmt)
1821+
appendCSVLiteral(&buf,debug_query_string);
1822+
appendStringInfoCharMacro(&buf,',');
1823+
if (print_stmt&&edata->cursorpos>0)
1824+
appendStringInfo(&buf,"%d",edata->cursorpos);
1825+
appendStringInfoCharMacro(&buf,',');
1826+
1827+
/* file error location */
1828+
if (Log_error_verbosity >=PGERROR_VERBOSE)
18321829
{
1833-
if (c=='"')
1834-
appendStringInfoCharMacro(buf,'"');
1835-
appendStringInfoCharMacro(buf,c);
1830+
StringInfoDatamsgbuf;
1831+
1832+
initStringInfo(&msgbuf);
1833+
1834+
if (edata->funcname&&edata->filename)
1835+
appendStringInfo(&msgbuf,"%s, %s:%d",
1836+
edata->funcname,edata->filename,
1837+
edata->lineno);
1838+
elseif (edata->filename)
1839+
appendStringInfo(&msgbuf,"%s:%d",
1840+
edata->filename,edata->lineno);
1841+
appendCSVLiteral(&buf,msgbuf.data);
1842+
pfree(msgbuf.data);
18361843
}
18371844

1838-
if (edata->cursorpos>0)
1839-
appendStringInfo(buf,_(" at character %d"),
1840-
edata->cursorpos);
1841-
elseif (edata->internalpos>0)
1842-
appendStringInfo(buf,_(" at character %d"),
1843-
edata->internalpos);
1845+
appendStringInfoChar(&buf,'\n');
18441846

1845-
appendStringInfoCharMacro(buf,'"');
1847+
/* If in the syslogger process, try to write messages direct to file */
1848+
if (am_syslogger)
1849+
write_syslogger_file(buf.data,buf.len,LOG_DESTINATION_CSVLOG);
1850+
else
1851+
write_pipe_chunks(buf.data,buf.len,LOG_DESTINATION_CSVLOG);
1852+
1853+
pfree(buf.data);
18461854
}
18471855

18481856
/*

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp