How to register a log in PHP?

10

I have a code in php that is doing checks on the server and when it is online or offline it returns a different message:

<?php
$server='LOJA 01 - ';
  $conectado = @ fsockopen('10.200.0.100', 135, $numeroDoErro, $stringDoErro, 1); // Este último é o timeout, em segundos
  if ($conectado) {
    echo "<font size=0> $server <img src=Verde.png height=8 width=8> </font>";
  } else {
    echo "<font size=0> $server <img src=Vermelho.png height=8 width=8> </font>";
  } 

?>

I would like to know if when it returns the second condition if PHP can record the date and time that happened in a location, which can be in syslog , the location could be on the same server as file .. Actually I I would like it to save the date and time it was an error for me to know from what time that link is off ...

    
asked by anonymous 11.09.2016 / 20:18

3 answers

8

Do this, use this function

function logMe($msg){
// Abre ou cria o arquivo bloco1.txt
// "a" representa que o arquivo é aberto para ser escrito
$fp = fopen("log.txt", "a");

// Escreve a mensagem passada através da variável $msg
$escreve = fwrite($fp, $msg);

// Fecha o arquivo
fclose($fp); -> OK
}

to use

logMe("Msg que vc quer guardar no txt");

If you still have questions, there is something much more explained here:

link

    
11.09.2016 / 20:30
9

You can use your own function for this, error_log , it basically writes the data in 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 certain file you can use it like this (changing the second parameter to 3 , by default it is 0):

error_log('Erro customizado' . PHP_EOL, 3, '/home/usuario/meulogdeerror.log');

In windows it should look something like:

error_log('Erro customizado' . PHP_EOL, 3, 'C:/wamp/log/meulogdeerror.log');

You may also want to send by email, so use 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 in 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 logging system, using the operating system logging system or to a file, depending on what is set in the error_log directive in php.ini. This is the default option.
  • 1 message is sent to the email address in $destination . This is the only type of message where the fourth parameter $extra_headers is used.
  • 2 No longer an option, I believe it was used only supported in PHP3
  • 3 message is added to the file set to $destination . A new line is not automatically added to the end of the string, which can make the messages stay on the same line, so you can use PHP_EOL as I showed 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.
10.10.2016 / 18:14
-6

I recommend using a class to create an error handling structure for your application and within one of the methods call the log function to save the error that occurred. This will bring quality into your code to prevent repetitions in your code.

    
10.10.2016 / 20:29