Skip to content

Commit 69d9a52

Browse files
authored
added setLogger() method to configure logging
2 parents 7092e6c + e3aaeba commit 69d9a52

File tree

3 files changed

+62
-7
lines changed

3 files changed

+62
-7
lines changed

README.md

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# ACMECert
22

33
PHP client library for [Let's Encrypt](https://letsencrypt.org/) and other [ACME v2 - RFC 8555](https://tools.ietf.org/html/rfc8555) compatible Certificate Authorities.
4-
Version: 3.2.2
4+
Version: 3.3.0
55

66
## Description
77

@@ -324,14 +324,23 @@ if ($days>30) { // renew 30 days before expiry
324324
325325
## Logging
326326

327-
ACMECert logs its actions using `error_log`, which logs messages to stderr per default in PHP CLI so it is easy to log to a file instead:
327+
By default ACMECert logs its actions using `error_log` which logs messages to stderr in PHP CLI so it is easy to log to a file instead:
328328
```php
329329
error_reporting(E_ALL);
330330
ini_set('log_errors',1);
331331
ini_set('error_log',dirname(__FILE__).'/ACMECert.log');
332332
```
333333

334-
334+
> To disable the default logging, you can use [`setLogger`](#acmecertsetlog), Exceptions are nevertheless thrown:
335+
```php
336+
$ac->setLogger(false);
337+
```
338+
> Or you can you set it to a custom callback function:
339+
```php
340+
$ac->setLogger(function($txt){
341+
echo 'Log Message: '.$txt."\n";
342+
});
343+
```
335344
## ACME_Exception
336345

337346
If the ACME-Server responded with an error message an `\skoerfgen\ACMECert\ACME_Exception` is thrown. (ACME_Exception extends Exception)
@@ -883,6 +892,29 @@ public array ACMECert::getTermsURL()
883892
884893
---
885894

895+
### ACMECert::setLogger
896+
897+
Turn on/off logging to stderr using `error_log` or provide a custom callback function.
898+
```php
899+
public void ACMECert::setLogger( bool|callable $value = TRUE )
900+
```
901+
###### Parameters
902+
> **`value`**
903+
>
904+
> - If `TRUE`, logging to stderr using `error_log` is enabled. (default)
905+
> - If `FALSE`, logging is disabled.
906+
> - If a callback function is provided, the function gets called with the log message as first argument:
907+
> ```php
908+
> void callback( string $txt )
909+
> ```
910+
> see [Logging](#logging)
911+
###### Return Values
912+
> No value is returned.
913+
###### Errors/Exceptions
914+
> Throws an `Exception` if the value provided is not boolean or a callable function.
915+
916+
---
917+
886918
> MIT License
887919
>
888920
> Copyright (c) 2018 Stefan Körfgen

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "skoerfgen/acmecert",
3-
"version": "3.2.2",
3+
"version": "3.3.0",
44
"description": "PHP client library for Let's Encrypt and other ACME v2 - RFC 8555 compatible Certificate Authorities",
55
"license": "MIT",
66
"authors": [

src/ACMEv2.php

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class ACMEv2 { // Communication with Let's Encrypt via ACME v2 protocol
3737
$directories=array(
3838
'live'=>'https://acme-v02.api.letsencrypt.org/directory',
3939
'staging'=>'https://acme-staging-v02.api.letsencrypt.org/directory'
40-
),$ch=null,$bits,$sha_bits,$directory,$resources,$jwk_header,$kid_header,$account_key,$thumbprint,$nonce=null;
40+
),$ch=null,$logger=true,$bits,$sha_bits,$directory,$resources,$jwk_header,$kid_header,$account_key,$thumbprint,$nonce=null;
4141
private $delay_until=null;
4242

4343
public function __construct($live=true){
@@ -113,8 +113,31 @@ public function getAccountID(){
113113
return $this->kid_header['kid'];
114114
}
115115

116+
public function setLogger($value=true){
117+
switch(true){
118+
case is_bool($value):
119+
break;
120+
case is_callable($value):
121+
break;
122+
default:
123+
throw new Exception('setLogger: invalid value provided');
124+
break;
125+
}
126+
$this->logger=$value;
127+
}
128+
116129
public function log($txt){
117-
error_log($txt);
130+
switch(true){
131+
case $this->logger===true:
132+
error_log($txt);
133+
break;
134+
case $this->logger===false:
135+
break;
136+
default:
137+
$fn=$this->logger;
138+
$fn($txt);
139+
break;
140+
}
118141
}
119142

120143
protected function create_ACME_Exception($type,$detail,$subproblems=array()){
@@ -286,7 +309,7 @@ private function http_request($url,$data=null){
286309
}
287310

288311
$method=$data===false?'HEAD':($data===null?'GET':'POST');
289-
$user_agent='ACMECert v3.2.2 (+https://github.com/skoerfgen/ACMECert)';
312+
$user_agent='ACMECert v3.3.0 (+https://github.com/skoerfgen/ACMECert)';
290313
$header=($data===null||$data===false)?array():array('Content-Type: application/jose+json');
291314
if ($this->ch) {
292315
$headers=array();

0 commit comments

Comments
 (0)