- PHP >= 7.1
- PHPMailer >= 6.0
Download it manually from GitHub or by composer: composer require mosetek/logger:dev-master
Initializing the logger
If you want to initialize logger with default path and filename (logs/.log
) just use:
$logger = new Logger();
If you want to initialize logger on another path with another filename you can do this by defining those while creating new object.
$logger = new Logger('some/path/', 'filename');
Also you can initialize logger with custom date format as the last parameter. Logger's using the standard PHP date() formats.
$logger = new Logger('some/path', 'filename', 'H:i:s');
Another path and filename options
You can get path, full path to file, filename or date format any time you need:
$logger->getPath(); //Returns: some/path/to/
$logger->getFullPath(); //Returns: some/path/to/file.txt
$logger->getFilename(); //Returns: file.txt
$logger->getDateFormat(); //Returns: Y-m-d H:i:s
You can also set another filename or path:
$logger->setPath('some/new/path');
$logger->setFilename('new_filename.txt');
$logger->setDateFormat('Y-m');
Write and read files
To put some content into file (defined in object creation) simply use log()
method.
You can log into file text, numbers and more complicated data structures like arrays and objects
$logger->log('some log content');
$logger->log(['some' => ['nice', 'array']);
You can also put some information into file, with some label next to it by adding information level as second parameter;
$logger->log('some log content', 3);
//That makes your log file looks like that:
// 2019-09-01 12:02:32 | [WARNING] | some log content
There are three supported labels for information leveling:
1 => [INFO]
2 => [DEBUG]
3 => [WARNING]
4 => [ERROR]
5 => [FATAL]
6 => [TRACE]
If you want to put data on different path you can declare it in third argument:
$logger->log('some log content on custom path', 0, 'custom/path/to/log.txt');
You can also put simple text without any date or special characters into your file by using text()
method.
$logger->text('Hello world');
text()
allows you to specify ending of line or text by adding additional param like that:
$logger->text('Hello world', ', ');
$logger->text('bye world.', PHP_EOL);
$logger->text('New line, world');
If you want to read contents of your log file use read($path): string
.
$logger->read();
You can also read content of any other text files by adding a path parameter to read method.
$logger->read('some/path/to/file.txt');
Putting variables into log file (deprecated: use log() instead)
You can put more complicated structures like variables into log file by using this:
$logger->dump(['some' => ['example', 'array']]);
Send file by email (deprecated)
You can send your log file by email use send($to)
. Method will grab contents of your log file and send it as message of email.
$logger->send('[email protected]');
Your server must support sendmail
to use the above. If you're using UNIX-like systems please take care of installing postfix.
Move and copy files
You can move your file to another destination.
$logger->move('new/path/to/file.log');
If you want to move another file than active log file use second argument to point what other file you want to move.
$logger->move('new/path/to/file.log', 'file/to/move.log');
If you want to just copy file some place else use copy()
method:
$logger->copy('where/to/copy/file.txt');
And similar to the move
method you can point another file to copy, if you don't want to copy your current log file.
$logger->copy('where/to/copy/file.txt', 'what/file/to/copy.txt);
Wiping and deleting log file
If you want to empty content of your current file use:
$logger->wipe();
You can also wipe any other text file by using $path
parameter:
$logger->wipe('some/path/to/file.txt');
Interface allows you to deleting files by using drop()
method.
$logger->drop();
Of course you can determine path to another file to drop as well
$logger->drop('some/path/to/file.txt');
Checking size of file
You can check the size of a file by using below:
$logger->getFilesize();
Method by default returns size in megabytes. You can determine another unit by passing argument.
$logger->getFilezise(); //Returns in megabytes
$logger->getFilezise('B'); //Returns in bytes
$logger->getFilezise('KB'); //Returns in kilobytes
$logger->getFilezise('GB'); //Returns in gigabytes
If you want you can check size of any other file by passing path in second argument:
$logger->getFilesize('MB', 'path/to/file.txt');