Last active
August 29, 2015 14:06
-
-
Save cvgellhorn/f4fd802ce0225b6573c9 to your computer and use it in GitHub Desktop.
Simple PHP log function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Write log message | |
* | |
* @param string $msg Log message | |
* @param string $file Filename | |
* @param bool $append Append or overwrite file | |
*/ | |
function log_msg($msg, $file = 'application.log', $append = true) | |
{ | |
$filePath = FS_LOGS . $file; | |
// Create dir recursive if not exists | |
$pathinfo = pathinfo($filePath); | |
if (!is_dir($pathinfo['dirname'])) { | |
@mkdir($pathinfo['dirname'], 0777, true); | |
} | |
$newFile = !file_exists($filePath); | |
// Write log | |
$content = date('Y-m-d H:i:s') . ': ' . $msg . PHP_EOL; | |
@file_put_contents($filePath, $content, ($append && !$newFile) ? FILE_APPEND : 0); | |
// Change new file permission | |
if ($newFile) @chmod($filePath, 0777); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment