PHP Log2Files Advanced Logger v 1.0This is main documentation file for Kemu Log2Files Advanced Logger v 1.0.IntroductionThis logger is an advanced library, which dumps PHP-Application logs to file in production environment.The main purpose is to ensure the logger is very fast and so can be used in production environment. Multiple PHP execution threads log to consecutive files at the same time, to avoid synchronization and locking problems. There is no need to do any additional setup. Just pure PHP (no mysql even!) and some simple API calls. WhyLogging in simple application, which is working for one user on a development server is extremely easy.Just open a file, write to it, close (PHP: fopen(), fwrite(), fclose) or even simply file_put_contents()... then open it in any text viewer, and its done - you've got the log. No library, nor framework is really needed. However, in real-time, multi-user, multi-script production environment, this won't work - simply because there are tens of scripts executing at the same time in different threads or even processes: ![]() For most of advanced applications, the scripts start and finish in very non-trivial (not to say "random") order: ![]() Logging engine needs synchronization - and the synchronization itself should have minimal impact to the script timings. The K_Log flow is synchronized only in one very small piece of code - during log construction it takes the unique number which is synchronized by flock(). So the script waits only a few micro-seconds or doesn't wait at all (if the lock is ready-to-take) - this is almost fastest possible method in PHP (don't even try to compare it to any mySQL-made logs). There is also some other synchronization point at the finish of a script, called "Log compacting and clean up" - but this part is executed after the script flushes its output, so it have almost no performance impact at all. SUMMARY: K_Log ensures that each execution log have its own unique id, is consistent, is not fragmented and is stored even if the PHP script aborts for some reason. Features
How it worksThere are some simple steps the K_Log follow:
ConfigurationThe configuration of the Logger should be provided in the constructor as an associative array (key=>value).There is only one mandatory field (logPath) which must be filled... All other fields are optional - they have default values. Ensure that all paths passed in config are writable by script. For your own convenience - give the logs they own separate directory (e.g. 'logs/log'). Config fields
Config fields in simple modeThese are ignored in advanced mode.
Configuration examplesThe simplest log possible:$log = new K_Log(array('logPath' => 'myLog')); would create a log object, which will log execution-log files as myLog000001, myLog000002, etc. and then will compact them to myLog_store_ filesThe not-so-simple code: $log = new K_Log( would create a log object, which will log execution-log files as data/logs/log000001, data/logs/log000002, etc. and then will compact them to data/logs/logstored_ files. Log stored files will be rotated (new one would be created) after 8 KiB (8*1024 bytes).array( 'logPath' => 'data/logs/log', 'storagePath' => 'data/logs/logstored_', 'storageFileMaxSize' => 8*1024, )); The config with specified delimeters: $log = new K_Log( would create a log object similar to previous one, but after each log entry the windows line-break ("\r\n") will be added. Also consecutive execution-logs will be separated by "----\r\n" string.array( 'logPath' => 'data/logs/log', 'storagePath' => 'data/logs/logstored_', 'entryDelimeter' => "\r\n", 'logSeparator' => "----\r\n" )); The config for BJSON logs: $log = new K_Log( would log in binary mode.array( 'logPath' => 'data/logs/log', 'advancedMode' => 1 )); UsageUsage examplesAfter construction of a log, simply call log() methods to add entries:$log->log("simple text"); // add text entry $log->log(1234); // add 1234 number $log->log("1234"); // add "1234" as string $log->logTime("executed 1st part of script"); // log the "elapsed time from execution start" in microseconds, with label "executed 1st part of script" $log->logKeyValue("theKey", "theValue"); // log the pair of $key => $value DemoThe link to online demo would be provided in next few days.API Referencenew K_Log($config)The constructor of K_Log.It interprets provided config, then starts log. Read more about $config in CONFIGURATION section. function generateFilePathForLog($idx)Returns the full path to log file of given index, using file-name generation template.function getCurrentLogFilePath()Returns the path to the file log is writing to.function getWholeLog()Returns the current execution log as a string (even in BJSON mode).This may be useful, because the file is flock'ed by the log, so direct file read may be impossible (especially on Windows servers, where flock is mandatory). function log($entry)Adds the entry to the log.function logArray($in)Logs array values (keys are ignored).This call forces the array to be dumped as "indexed array" - so keys simply are ignored - opposite is logMap() call. function logMap($in)Logs array as map of key=>value.function logAsString($entry)Adds the entry to the log as string.This call forces conversion of variable to string before adding to log. function logAsInt($entry)Adds the entry to the log as int.This call forces conversion of variable to integer before adding to log. function logKeyValue($key, $value)Adds the entry to the log as a pair key=>value.The $key and $value may be any type. In BJSON mode, this is encoded as a map with only one key=>value entry. function logTime($label)Logs the named timestamp.The time is number of microseconds elapsed from log start (the log start time may be overriden by setting "startTime" in configuration passed during log construction). In BJSON mode, this is encoded as an array with three elements: - the hardcoded string 'time', - the $label, - the integer time, function shutdown()A PHP shutdown callback - It is not a log finish function !This should never be called directly - it's called after PHP script finishes. OtherFAQQ: Can I have two or more logs created in one script ?A: Yes ! You may have as many different logs as you want - each configured independently. E.g. you may log user actions to one log, and some time-stats to different one. Just ensure they have different filenames or filepaths - the 'logPath' field in configuration passed to constructor. Q: Why there are no timestamps in each log entry ? A: Auto-adding timestamps into each entry would improve the profiling abilities of the log. However, this would also make log a lot bigger, harder to parse and the logging itself would be slower. You can add a timestamp by simply calling logTime(). RoadmapThe Kemu Log2Files is under continous improvement process.We will soon: - provide the online demo of K_Log, - publish some performance tests and comparison to mysql calls, LicenseAll copyrights reserved by Kemu Studio, Pietrzak 'yosh' Roman (visit yosh.ke.mu).Source code and documentation available from CodeCanyon |