You can use a proper function for this, the error_log, it basically writes the data to the file defined in php.ini:
error_log('Erro customizado'); // mesmo que: error_log('Erro customizado', 0);
But if you want to write to a particular file you can use it (changing the second parameter to 3
, by default it is 0):
error_log('Erro customizado' . PHP_EOL, 3, '/home/usuario/meulogdeerror.log');
In windows should be something like:
error_log('Erro customizado' . PHP_EOL, 3, 'C:/wamp/log/meulogdeerror.log');
You may also want to send by email, for this use the parameter 1:
error_log('Erro customizado', 1, '[email protected]');
Note that in this case it is necessary to configure the fourth parameter called $extra_headers
, this last parameter is only used for the case of emails
bool error_log ( string $message [, int $message_type = 0 [, string $destination [, string $extra_headers ]]] )
You have 4 log types as you set the second parameter $message_type
:
0
message is sent to the PHP log system, using the operating system log system or to a file, depending on what is defined in the directive error_log
in php.ini. This is the default option.
1
message is sent to the email address at $destination
. This is the only message type where the fourth parameter $extra_headers
is used.
2
It is no longer an option, I believe it was used supported only in PHP3
3
message is added to the file set in $destination
. A new line is not automatically added to the end at the end of the string, which can make the messages stay on the same line, so you can use PHP_EOL
as shown in the examples above.
4
the message will be sent directly to the SAPI log, I believe that if you use apache it will go to the Apache log.
Where do you want to save this log? What location? Like a file, in syslog? How will you analyze it later ?
– gmsantos
can be in a syslog, the location could be on the same server as archive.. In fact I would like him to save the time when gave error for me to know since what time that link is out...
– Igor Santana
Cool Igor, recommend [Dit] your question and put what you said in the comment to help those in the future get here. Your question is good but may improve in some points. Recommended reading: [mcve] and [tour]
– gmsantos
No need to close the question. The question is simple and clear. One way to solve is by using the native function
error_log('mensagem aqui', 3, '/onde/salvar/arquivo.log');
. See: http://php.net/error_log– Daniel Omine
A function of PHP itself can be very useful for this. http://php.net/manual/en/function.error-log.php
– hoheckell
No need to put "solved", just mark that answer that solved the problem the green light. More details on: How and why to accept an answer?
– rray
I’m putting a bonus to the @Guilhermenascimento response, which I understand using the correct resource for what was requested.
– Bacco