Generate system logs in PHP [closed]

1

I need to generate a text document of type .txt containing information about changes made to a web system. This information is about user activities such as registrations, change of registrations, removal of registrations and among others. How can this be done considering that the system in question is being implemented with PHP language?
I thought about using the observer pattern, but I'm not sure if it's the best option. Is there any specific design pattern or framework for this purpose?

    
asked by anonymous 14.05.2017 / 21:58

1 answer

2

I do not know any frameworks, but it's very simple to do. I created the below sample function using fopen . In the function I put the files separated by date. In each line of the file it has the record of the times in which the logs occurred.

function logs($texto){

        $hora = date("H:i:s"); // pega a hora
        $data = date("d-m-Y"); // pega o dia
        /*

            o "a+" abaixo significa:
            - Abre o arquivo para leitura e gravação; 
            - coloca o ponteiro no fim do arquivo. 
            - Se o arquivo não existir, tentar criá-lo.

        */
        $log = fopen("log/".$data.".txt", "a+");

        $escreve = fwrite($log, $hora." - ".$texto);// Escreve

        fclose($log); // Fecha o arquivo

    }

When something happens in the system, for example a change of register, you send this information using the logs function previously created. So:

function alterarCadatro($nome, $email, $senha){

    ... mysqli_query...
    /*
        depois que finalizou a alteração do cadastro,
        envia a informação para a função logs
    */
    if($alterou){

        require("logs.php");
        $texto = $nome." alterou o cadastro nos campos: ".$email.",".$senha;
        logs($texto);

    }

    ...
}

Then you just create your patterns, for example:

hora - tipo - local - quem

It would look like this:

00:25:05 - alteração - cadastro - Andrei
  

Note: This is just an example, as was quoted by @Inkeliz, it is   necessary to make filters, satinize and / or create the necessary   security code.

    
15.05.2017 / 05:21