Skip to content

Commit

Permalink
options
Browse files Browse the repository at this point in the history
  • Loading branch information
muhammetsafak committed Dec 15, 2023
1 parent d9b80dc commit c95abc9
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use \InitPHP\Logger\FileLogger;

$logFile = __DIR__ . '/logfile.log';

$logger = new Logger(new FileLogger($logFile));
$logger = new Logger(new FileLogger(['path' => $logFile]));
```

### PdoLogger
Expand All @@ -46,7 +46,7 @@ use \InitPHP\Logger\PDOLogger;
$table = 'logs';
$pdo = new \PDO('mysql:dbname=project;host=localhost', 'root', '');

$logger = new Logger(new PDOLogger($pdo, $table));
$logger = new Logger(new PDOLogger(['pdo' => $pdo, 'table' => $table]));

$logger->error('User {user} caused an error.', array('user' => 'muhametsafak'));
// INSERT INTO logs (level, message, date) VALUES ('ERROR', 'User muhametsafak caused an error.', '2022-03-11 13:05:45')
Expand Down Expand Up @@ -75,7 +75,7 @@ $logFile = __DIR__ . '/logfile.log';
$table = 'logs';
$pdo = new \PDO('mysql:dbname=project;host=localhost', 'root', '');

$logger = new Logger(new FileLogger($logFile), new PDOLogger($pdo, $table));
$logger = new Logger(new FileLogger(['path' => $logFile]), new PDOLogger(['pdo' => $pdo, 'table' => $table]));
```

## Methods
Expand Down
6 changes: 3 additions & 3 deletions src/FileLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ class FileLogger extends \Psr\Log\AbstractLogger implements \Psr\Log\LoggerInter
protected $path;

/**
* @param string $path
* @param array $options
*/
public function __construct($path)
public function __construct(array $options = [])
{
$this->path = $this->interpolate($path, array(
$this->path = $this->interpolate($options['path'], array(
'year' => date('Y'),
'month' => date('m'),
'day' => date('d'),
Expand Down
13 changes: 8 additions & 5 deletions src/PDOLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,19 @@ class PDOLogger extends \Psr\Log\AbstractLogger implements \Psr\Log\LoggerInterf
/** @var string */
protected $table;

public function __construct($pdo, $table)
/**
* @param array $options
*/
public function __construct(array $options = [])
{
if(!($pdo instanceof PDO)){
if(!($options['pdo'] instanceof PDO)){
throw new \InvalidArgumentException('It must be a PDO object.');
}
if(!is_string($table)){
if(!is_string($options['table'])){
throw new \InvalidArgumentException('The name of the table where the logs will be kept must be specified as a string.');
}
$this->pdo = $pdo;
$this->table = $table;
$this->pdo = $options['pdo'];
$this->table = $options['table'];
}

public function __destruct()
Expand Down

0 comments on commit c95abc9

Please sign in to comment.