Обсуждение: psql -L log errors

Поиск
Список
Период
Сортировка

psql -L log errors

От
Hans Ginzel
Дата:
Hello!

There is, in documentation of psql client (http://www.postgresql.org/docs/9.4/static/app-psql.html):
--log-file=filename
     Write all query output into file filename, in addition to the normal output destination.

But it seems, that psql logs only the query output (and input if -e or -a),
but does not write the error output. How to also log the errors, please?

Thanks
Hans


Re: psql -L log errors

От
Felipe Santos
Дата:


2015-03-11 6:00 GMT-03:00 Hans Ginzel <hans@matfyz.cz>:
Hello!

There is, in documentation of psql client (http://www.postgresql.org/docs/9.4/static/app-psql.html):
--log-file=filename
    Write all query output into file filename, in addition to the normal output destination.

But it seems, that psql logs only the query output (and input if -e or -a),
but does not write the error output. How to also log the errors, please?

Thanks
Hans


--
Sent via pgsql-novice mailing list (pgsql-novice@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-novice


Hi Hans,

It's the operating system "fault" .

The normal output and the error output are different things, although we experience them as the same in most of the cases.

The OS sends both normal and error output to the standard output device, which is usually our display/screen.

When you are using the "--log-file" clause, you are telling the psql to redirect the normal output to the file indicated, but the error output keeps being sent to the standard output device.

To redirect the error output you have to append it to the file you're logging into.

To do that, do the following (both Win and Linux, replace the psql arguments ofr yours) :
psql -U sample_user -W -c "select * from foo;" sample_database --log-file=sample_file.txt 2>> sample_file.txt

The part that accomplishes the "trick" is this:
2>> sample_file.txt

The ">>" sign tells the OS to append to the file, if you use ">" it will overwrite it, and you dont want to overwrite it because you are already writing on it with the "--log-file" clause.

Best regards,

Felipe


Re: psql -L log errors

От
Hans Ginzel
Дата:
On Wed, Mar 11, 2015 at 08:47:13AM -0300, Felipe Santos wrote:
>   When you are using the "--log-file" clause, you are telling the psql to
>   redirect the normal output to the file indicated, but the error output
>   keeps being sent to the standard output device.
>   To redirect the error output you have to append it to the file you're
>   logging into.
>   To do that, do the following (both Win and Linux, replace the psql
>   arguments ofr yours) :
>   psql -U sample_user -W -c "select * from foo;" sample_database
>   --log-file=sample_file.txt 2>> sample_file.txt

Thank you for your answer, Felipe.
Why the error output keeps being sent to the standard output device?
Does it get sense to redirect only stdout to file when "--log-file" is used?
Why not to also redirect stderr? Could the dup2() fuction be used for that?
Or even better could the stderr be "teed" both to the log file and to the standard output device?

Thanks
Hans


http://stackoverflow.com/questions/14543443/in-c-how-do-you-redirect-stdin-stdout-stderr-to-files-when-making-an-execvp-or
https://github.com/goj/coreutils/blob/rm-d/src/tee.c



Re: psql -L log errors

От
Felipe Santos
Дата:

2015-03-11 11:01 GMT-03:00 Hans Ginzel <hans@matfyz.cz>:
On Wed, Mar 11, 2015 at 08:47:13AM -0300, Felipe Santos wrote:
  When you are using the "--log-file" clause, you are telling the psql to
  redirect the normal output to the file indicated, but the error output
  keeps being sent to the standard output device.
  To redirect the error output you have to append it to the file you're
  logging into.
  To do that, do the following (both Win and Linux, replace the psql
  arguments ofr yours) :
  psql -U sample_user -W -c "select * from foo;" sample_database
  --log-file=sample_file.txt 2>> sample_file.txt

Thank you for your answer, Felipe.
Why the error output keeps being sent to the standard output device?
Does it get sense to redirect only stdout to file when "--log-file" is used?
Why not to also redirect stderr? Could the dup2() fuction be used for that?
Or even better could the stderr be "teed" both to the log file and to the standard output device?

Thanks
Hans

http://stackoverflow.com/questions/14543443/in-c-how-do-you-redirect-stdin-stdout-stderr-to-files-when-making-an-execvp-or
https://github.com/goj/coreutils/blob/rm-d/src/tee.c



Why the error output keeps being sent to the standard output device?
Does it get sense to redirect only stdout to file when "--log-file" is used?
Because the error output is an operating system generic error output, it means that ANY kind of error will be redirected to it, not only your SQL Statements errors, so I think it makes sense to split them (my opinion only)


Why not to also redirect stderr? Could the dup2() fuction be used for that?
Really don't know


Or even better could the stderr be "teed" both to the log file and to the standard output device?
Well, one way to achieve that is redirecting everything to the same file and (on Linux and in another session) calling "tail -f sample_file.txt" to watch the actual progress of your process. I am sure there must be other ways, but this is the one I use the most.