Coder Social home page Coder Social logo

mateusz-osetek / php-logger Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 0.0 32 KB

Simple PHP interface for creating and managing text logs and files

Home Page: https://packagist.org/packages/mosetek/logger

PHP 100.00%
log logger file files logging logs filesystem text interface php composer composer-package composer-library library universal-logger errors error-log errorslog error-logging error-logger

php-logger's Introduction

Simple PHP Logger 1.1.3

Requirements

  • PHP >= 7.1
  • PHPMailer >= 6.0

Installation

Download it manually from GitHub or by composer: composer require mosetek/logger:dev-master


Usage

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');

php-logger's People

Contributors

mateusz-osetek avatar vanguardkras avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.