Download the PHP package stange/slog without Composer
On this page you can find all versions of the php package stange/slog. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package slog
Slog (Simple Log)
A simple PHP logging class
Preface
Slog is a simple PHP logging class which you can easily include in any of your projects.
Installing Slog
You can clone the latest version from GIT
Or just add Slog to your project via composer
Features
- [x] ANSI output coloring
- [x] Log to a file
- [x] Log tags (see below)
- [x] Prepend or append a log string
- [x] Prefix your log messages with a specific date format
Examples
Basic Example
Using specific log types types and ANSI coloring capabilities
Slog contains different methods for you to inform different types of log messages. Also, by default, SLog will colorize your log output
Result:
You can of course, disable output coloring
Log to a file
NOTE: ANSI colors will not be logged into the file
File only output example
In case you don't want to output to stdout, you can pass in 'echo'=>FALSE in the constructor or disable stdout output by using Slog::setEcho(FALSE).
Log Tagging
Logging is a great thing, however sometimes the amount of logged information can be cumbersome.
A cool feature of Slog is that you can tag your logs.
Tag your logs? What do you mean?
I mean that you can use certain "tags" in your log messages for logging messages of a certain kind but not others of a different kind.
Here is a brief example of two tagged log messages:
In the previous example, both messages will be logged and shown through stdout.
However, now, if we add a log tag to slog things will be a bit different.
Only messages which match the tag "tagOne" will be logged. Messages containing the tag "tagTwo" will be discarded, i.e: not logged.
You can log multiple tags if you need to, this would be a good idea when you want to increase your log verbosity
Adding or removing log tags on the run
You can add or remove a tag at any given point in time through the methods:
- Slog::removeTag($tag) Removes a tag
- Slog::addTag($tag) Specifies that messages containing a tag should be logged
- Slog::unsetTags() Log everything
Loggable Trait and Loggable Interface
As an extra, I have included a simple logging trait which will enable you to use said trait in your own classes to make said class "Loggable", this trait contains three main methods:
- Loggable::setLog(LogInterface $log)
- Loggable::getLog()
- Loggable::log($message,$type=NULL)
NOTE: The trait will also prepend the CLASS name to the log, in this way, if you have multiple classes which make use of the loggable trait, you can identify which logs come from one class and which other logs come from another
Trait and Loggable interface implementation example
With the previous code we could easily do the following implementation
TODO (next release)
- [ ] Create an adapter pattern for logging messages in different places
- [ ] Add logging adapters (Socket, File, Memory, etc)