summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKenny Katzgrau <katzgrau@gmail.com>2015-04-13 10:32:55 -0400
committerKenny Katzgrau <katzgrau@gmail.com>2015-04-13 10:32:55 -0400
commit6477bd292ca88560e99e1ac054db74424c3c105d (patch)
treeab632fb26b73811a332f700a6641d82690adfdf2
parentd7309d0520887dc0a0bd361c4d0da0131b4614a1 (diff)
parenta67677020ccb47d8dec6bd2a49d75ece01883c93 (diff)
downloadKLogger-6477bd292ca88560e99e1ac054db74424c3c105d.zip
KLogger-6477bd292ca88560e99e1ac054db74424c3c105d.tar.gz
KLogger-6477bd292ca88560e99e1ac054db74424c3c105d.tar.bz2
Merge pull request #52 from onno-vos-dev/CleanConstructor
Clean constructor
-rwxr-xr-xsrc/Logger.php53
1 files changed, 39 insertions, 14 deletions
diff --git a/src/Logger.php b/src/Logger.php
index c95d4fa..4dfcb3d 100755
--- a/src/Logger.php
+++ b/src/Logger.php
@@ -112,28 +112,20 @@ class Logger extends AbstractLogger
$this->logLevelThreshold = $logLevelThreshold;
$this->options = array_merge($this->options, $options);
- $logDirectory = rtrim($logDirectory, '\\/');
- if (! file_exists($logDirectory)) {
+ $logDirectory = rtrim($logDirectory, DIRECTORY_SEPARATOR);
+ if ( ! file_exists($logDirectory)) {
mkdir($logDirectory, $this->defaultPermissions, true);
}
if($logDirectory === "php://stdout" || $logDirectory === "php://output") {
- $this->logFilePath = $logDirectory;
- $this->fileHandle = fopen($this->logFilePath, 'w+');
+ $this->setLogToStdOut($logDirectory);
+ $this->setFileHandle('w+');
} else {
- if ($this->options['filename']) {
- $this->logFilePath = $logDirectory.DIRECTORY_SEPARATOR.$this->options['filename'];
- } else {
- $this->logFilePath = $logDirectory.DIRECTORY_SEPARATOR.$this->options['prefix'].date('Y-m-d').'.'.$this->options['extension'];
- }
-
+ $this->setLogFilePath($logDirectory);
if(file_exists($this->logFilePath) && !is_writable($this->logFilePath)) {
throw new RuntimeException('The file could not be written to. Check that appropriate permissions have been set.');
}
- $this->fileHandle = fopen($this->logFilePath, 'a');
- if(!$this->fileHandle) {
- throw new RuntimeException('The file could not be opened. Check permissions.');
- }
+ $this->setFileHandle('a');
}
if ( ! $this->fileHandle) {
@@ -142,6 +134,39 @@ class Logger extends AbstractLogger
}
/**
+ * @param string $stdOutPath
+ */
+ public function setLogToStdOut($stdOutPath) {
+ $this->logFilePath = $stdOutPath;
+ }
+
+ /**
+ * @param string $logDirectory
+ */
+ public function setLogFilePath($logDirectory) {
+ if ($this->options['filename']) {
+ if (strpos($this->options['filename'], '.log') !== false || strpos($this->options['filename'], '.txt') !== false) {
+ $this->logFilePath = $logDirectory.DIRECTORY_SEPARATOR.$this->options['filename'];
+ }
+ else {
+ $this->logFilePath = $logDirectory.DIRECTORY_SEPARATOR.$this->options['filename'].'.'.$this->options['extension'];
+ }
+ } else {
+ $this->logFilePath = $logDirectory.DIRECTORY_SEPARATOR.$this->options['prefix'].date('Y-m-d').'.'.$this->options['extension'];
+ }
+ }
+
+ /**
+ * @param $writeMode
+ *
+ * @internal param resource $fileHandle
+ */
+ public function setFileHandle($writeMode) {
+ $this->fileHandle = fopen($this->logFilePath, $writeMode);
+ }
+
+
+ /**
* Class destructor
*/
public function __destruct()