-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit a1e0e27
Showing
8 changed files
with
431 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
composer.phar | ||
vendor/ | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#PHPMailer logger for Apix Log | ||
|
||
An extension for the [Apix/Log](https://github.com/frqnck/apix-log) PSR-3 logger that sends log messages via email using [PHPMailer](https://github.com/PHPMailer/PHPMailer). | ||
|
||
##Usage | ||
|
||
Create an Apix PhpMailer Log instance, providing a pre-configured PHPMailer instance to the constructor. | ||
This instance will be used for all subsequent messages. | ||
|
||
By default the logger sends an email for each individual log message received, which can be quite inefficient, so call `$logger->setDeferred(true)` to save up the log messages and send them all in one message on `__destruct`. | ||
|
||
We suggest you enable exceptions in your PHPMailer instance otherwise you may not be told about problems sending your log messages. | ||
|
||
##Example | ||
|
||
```php | ||
// Create a PHPMailer instance with exceptions enabled | ||
$mailer = new \PHPMailer(true); | ||
$mailer->addAddress('[email protected]', 'Log Mailbox'); | ||
$mailer->setFrom('[email protected]', 'My App'); | ||
$mailer->isSMTP(); | ||
$mailer->SMTPAuth = true; | ||
$mailer->Host = 'tls://mail.example.com:587'; | ||
$mailer->Username = 'user'; | ||
$mailer->Password = 'pass'; | ||
$mailer->isHTML(false); | ||
$mailer->Subject = 'Error log'; | ||
|
||
$logger = new Apix\Logger\PhpMailer($mailer); | ||
$logger->setDeferred(true); | ||
$logger->info('Log me!'); | ||
$logger->error('Log me too!'); | ||
``` |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"name": "phpmailer/apix-log-phpmailer", | ||
"description": "A PHPMailer logger for Apix log", | ||
"type": "library", | ||
"minimum-stability": "stable", | ||
"license": "BSD-3-Clause", | ||
"authors": [ | ||
{ | ||
"name": "Marcus Bointon", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"require": { | ||
"php": ">=5.3", | ||
"apix/log": "^1.1", | ||
"phpmailer/phpmailer": "^5.2" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"Apix\\Log\\Logger\\": "src/", | ||
"Apix\\Log\\Logger\\tests\\": "tests/" | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<phpunit | ||
backupGlobals="false" | ||
backupStaticAttributes="false" | ||
colors="false" | ||
convertErrorsToExceptions="true" | ||
convertNoticesToExceptions="true" | ||
convertWarningsToExceptions="true" | ||
processIsolation="false" | ||
stopOnFailure="false" | ||
syntaxCheck="false" | ||
bootstrap="tests/bootstrap.php" | ||
> | ||
<testsuites> | ||
<testsuite name="Apix Log Unit Tests"> | ||
<directory suffix="Test.php">tests</directory> | ||
</testsuite> | ||
</testsuites> | ||
|
||
</phpunit> | ||
|
||
<!-- vim: set tabstop=4 shiftwidth=4 expandtab: --> |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
/** | ||
* Apix PHPMailer logger. | ||
* @license http://opensource.org/licenses/BSD-3-Clause New BSD License | ||
* @author Marcus Bointon <[email protected]> | ||
*/ | ||
|
||
namespace Apix\Log\Logger; | ||
|
||
use Apix\Log\Exception; | ||
use Psr\Log\InvalidArgumentException; | ||
|
||
/** | ||
* Apix logger for sending logs via PHPMailer. | ||
* | ||
* @author Marcus Bointon <[email protected]> | ||
*/ | ||
class PhpMailer extends AbstractLogger implements LoggerInterface | ||
{ | ||
/** | ||
* A PHPMailer instance to use for sending. | ||
* @type \PHPMailer | ||
*/ | ||
protected $phpmailer; | ||
|
||
/** | ||
* Constructor. | ||
* Note PHPMailer is *NOT* namespaced. | ||
* @param \PHPMailer $phpmailer A preconfigured PHPMailer instance to use for sending. | ||
*/ | ||
public function __construct($phpmailer) | ||
{ | ||
if (!$phpmailer instanceof \PHPMailer) { | ||
throw new InvalidArgumentException( | ||
'Must be an instance of \PHPMailer', 1 | ||
); | ||
} | ||
|
||
if (count($phpmailer->getToAddresses()) < 1) { | ||
throw new InvalidArgumentException( | ||
'No valid email address set in \PHPMailer' | ||
); | ||
} | ||
|
||
$this->phpmailer = $phpmailer; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function write(array $log) | ||
{ | ||
$this->phpmailer->Body = $log['msg']; | ||
try { | ||
return $this->phpmailer->send(); | ||
} catch (\phpmailerException $e) { | ||
throw new Exception($e->getMessage(), $e->getCode(), $e); | ||
} | ||
} | ||
} |
Oops, something went wrong.