- Notifications
You must be signed in to change notification settings - Fork0
Simple PHP interface for creating and managing text logs and files
mateusz-osetek/php-logger
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
- 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 =newLogger();
If you want to initialize logger on another path with another filename you can do this by defining those while creating new object.
$logger =newLogger('some/path/','filename');
Also you can initialize logger with custom date format as the last parameter. Logger's using thestandard PHP date() formats.
$logger =newLogger('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 uselog()
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 usingtext()
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 useread($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 usesend($to)
. Method will grab contents of your log file and send it as message of email.
$logger->send('somebody@email.com');
Your server must supportsendmail
to use the above. If you're using UNIX-like systems please take care of installingpostfix.
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 usecopy()
method:
$logger->copy('where/to/copy/file.txt');
And similar to themove
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 usingdrop()
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');
About
Simple PHP interface for creating and managing text logs and files