How to log in PHP?

Asked

Viewed 9,773 times

10

I have a php code that keeps checking 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 wonder if when it returns the second condition if PHP can record the date and time that happened in a location, which can be in a syslog, the location could be on the same server as file.. In fact I would like it to keep the date and time when it gave error for me to know since what time that link is out...

  • Where do you want to save this log? What location? Like a file, in syslog? How will you analyze it later ?

  • 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...

  • 1

    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]

  • 2

    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

  • A function of PHP itself can be very useful for this. http://php.net/manual/en/function.error-log.php

  • 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?

  • 2

    I’m putting a bonus to the @Guilhermenascimento response, which I understand using the correct resource for what was requested.

Show 2 more comments

3 answers

11

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.
  • Can I add to any PHP query? And put that code right after? ?

  • 1

    @Pauloboadventure in any part of PHP code, which will depend on you ;)

11


Do it like 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 doubts have something well explained here :

http://rberaldo.com.br/como-gerar-logs-execucao-php/

  • 1

    Thank you so much! !!!

  • glad to know I helped, understood how the process works?

  • 1

    does not forget to mark as completed when finishing

  • Got it!!!! Thank you very much!!! Now I don’t know how to finish the question :S I’m a beginner here!! rsrsrs

-6

I recommend using a class to create an error handling structure of your application and within one of the methods call the log function to save the error. This will bring quality in your code to avoid repeats in your code.

  • 2

    You mean quality code is OOP? And procedural codes have inferior quality?

  • 4

    It would be nice an example of the code to see the advantages and the quality obtained, in view of the native function.

  • 2

    Please edit your question. Its quality is low. Give more details, provide examples.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.