-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwrite_file_php.php
More file actions
43 lines (36 loc) · 1.06 KB
/
Copy pathwrite_file_php.php
File metadata and controls
43 lines (36 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
class writeMsg
{
static function logMsg ( $msg, $level = 'info', $file = 'main.log' )
{
// variável que vai armazenar o nível do log (INFO, WARNING ou ERROR)
$levelStr = '';
// verifica o nível do log
switch ( $level )
{
case 'info':
// nível de informação
$levelStr = 'INFO';
break;
case 'warning':
// nível de aviso
$levelStr = 'WARNING';
break;
case 'error':
// nível de erro
$levelStr = 'ERROR';
break;
}
// data atual
$date = date ( 'Y-m-d H:i:s' );
// formata a mensagem do log
// 1o: data atual
// 2o: nível da mensagem (INFO, WARNING ou ERROR)
// 3o: a mensagem propriamente dita
// 4o: uma quebra de linha
$msg = sprintf( "[%s] [%s]: %s%s", $date, $levelStr, $msg, PHP_EOL );
// escreve o log no arquivo
// é necessário usar FILE_APPEND para que a mensagem seja escrita no final do arquivo, preservando o conteúdo antigo do arquivo
file_put_contents( $file, $msg, FILE_APPEND );
}
}
teste::logMsg("opa");