Skip to content

Instantly share code, notes, and snippets.

@cvgellhorn
Last active August 29, 2015 14:06
Show Gist options
  • Save cvgellhorn/f4fd802ce0225b6573c9 to your computer and use it in GitHub Desktop.
Save cvgellhorn/f4fd802ce0225b6573c9 to your computer and use it in GitHub Desktop.
Simple PHP log function
<?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