33namespace Koded \Logging \Processors ;
44
55use Exception ;
6+ use Koded \Exceptions \KodedException ;
67
78/**
89 * Log processor for storing the log messages into text files on the disk.
2223class File extends Processor
2324{
2425
26+ const E_DIRECTORY_DOES_NOT_EXIST = 1 ;
27+ const E_DIRECTORY_NOT_WRITABLE = 2 ;
28+ const E_DIRECTORY_NOT_CREATED = 3 ;
29+
2530 /**
2631 * @var string The log filename.
2732 */
@@ -52,19 +57,19 @@ protected function initialize(array $settings)
5257 ) . DIRECTORY_SEPARATOR ;
5358
5459 if (!is_dir ($ dir )) {
55- throw new Exception ( sprintf ( ' Log directory "%s" must exist. ' , $ dir) );
60+ throw new FileProcessorException ( self :: E_DIRECTORY_DOES_NOT_EXIST , [ ' :dir ' => $ dir] );
5661 }
5762
5863 if (!is_writable ($ dir )) {
59- throw new Exception ( sprintf ( ' Log directory "%s" must be writable. ' , $ dir) );
64+ throw new FileProcessorException ( self :: E_DIRECTORY_NOT_WRITABLE , [ ' :dir ' => $ dir] );
6065 }
6166
6267 $ dir = sprintf ('%s%s%s ' , $ dir , date ('Y/m ' ), DIRECTORY_SEPARATOR );
6368
6469 if (!is_dir ($ dir )) {
6570 if (false === @mkdir ($ dir , 0777 , true )) {
6671 // @codeCoverageIgnoreStart
67- throw new Exception ( sprintf ( ' Failed to create a log directory "%s". ' , $ dir) );
72+ throw new FileProcessorException ( self :: E_DIRECTORY_NOT_CREATED , [ ' :dir ' => $ dir] );
6873 // @codeCoverageIgnoreEnd
6974 }
7075 }
@@ -77,3 +82,12 @@ protected function parse(array $message)
7782 file_put_contents ($ this ->filename , strtr ($ this ->format , $ message ) . PHP_EOL , FILE_APPEND );
7883 }
7984}
85+
86+ class FileProcessorException extends KodedException
87+ {
88+ protected $ messages = [
89+ File::E_DIRECTORY_DOES_NOT_EXIST => 'Log directory ":dir" must exist. ' ,
90+ File::E_DIRECTORY_NOT_WRITABLE => 'Log directory ":dir" must be writable. ' ,
91+ File::E_DIRECTORY_NOT_CREATED => 'Failed to create a log directory ":dir". ' ,
92+ ];
93+ }
0 commit comments