Skip to content

Commit 775035a

Browse files
committed
improve log system
1 parent 4e58264 commit 775035a

File tree

6 files changed

+17
-81
lines changed

6 files changed

+17
-81
lines changed

docs/contribute.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ docker-compose run wordpress<X.X> bash
135135
### Display the plugin logs
136136

137137
```bash
138-
tail -f logs/*
138+
tail -f logs/debug-*
139139
```
140140

141141
#### New feature workflow

inc/bouncer-instance.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use CrowdSecBouncer\Bouncer;
44
use CrowdSecBouncer\BouncerException;
55
use CrowdSecBouncer\Constants;
6+
use Monolog\Formatter\LineFormatter;
67
use Monolog\Handler\RotatingFileHandler;
78
use Monolog\Logger;
89
use Symfony\Component\Cache\Adapter\AbstractAdapter;
@@ -25,15 +26,18 @@ function getCrowdSecLoggerInstance(): Logger
2526

2627
// Log more data if WP_DEBUG=1
2728

28-
$loggerLevel = WP_DEBUG ? Logger::DEBUG : Logger::INFO;
2929
$logger = new Logger('wp_bouncer');
30-
$fileHandler = new RotatingFileHandler(CROWDSEC_LOG_PATH, 0, $loggerLevel);
3130

32-
// Set custom readble logger for WP_DEBUG=1
31+
$fileHandler = new RotatingFileHandler(CROWDSEC_LOG_PATH, 0, Logger::INFO);
32+
$fileHandler->setFormatter(new LineFormatter("%datetime%|%level%|%context%\n"));
33+
$logger->pushHandler($fileHandler);
34+
35+
// Set custom readable logger for WP_DEBUG=1
3336
if (WP_DEBUG) {
34-
$fileHandler->setFormatter(new \Bramus\Monolog\Formatter\ColoredLineFormatter(null, "[%datetime%] %message% %context%\n", 'H:i:s'));
37+
$debugFileHandler = new RotatingFileHandler(CROWDSEC_DEBUG_LOG_PATH, 0, Logger::DEBUG);
38+
$debugFileHandler->setFormatter(new \Bramus\Monolog\Formatter\ColoredLineFormatter(null, "[%datetime%] %message% %context%\n", 'H:i:s'));
39+
$logger->pushHandler($debugFileHandler);
3540
}
36-
$logger->pushHandler($fileHandler);
3741

3842
return $logger;
3943
}

inc/constants.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22

3-
define('CROWDSEC_LOG_PATH', CROWDSEC_PLUGIN_PATH.'/logs/crowdsec.log');
3+
$crowdsecRandomLogFolder = get_option('crowdsec_random_log_folder') ?: '';
4+
define('CROWDSEC_LOG_PATH', CROWDSEC_PLUGIN_PATH."/logs/$crowdsecRandomLogFolder/prod.log");
5+
define('CROWDSEC_DEBUG_LOG_PATH', CROWDSEC_PLUGIN_PATH."/logs/$crowdsecRandomLogFolder/debug.log");
46
define('CROWDSEC_CACHE_PATH', CROWDSEC_PLUGIN_PATH.'/.cache');
57

68
define('CROWDSEC_BOUNCING_LEVEL_DISABLED', 'bouncing_disabled');

inc/plugin-setup.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ function activate_crowdsec_plugin()
3131
update_option('crowdsec_hide_mentions', false);
3232
update_option('crowdsec_trust_ip_forward', '');
3333
update_option('crowdsec_trust_ip_forward_array', []);
34+
35+
if (!get_option('crowdsec_random_log_folder')) {
36+
update_option('crowdsec_random_log_folder', bin2hex(random_bytes(64)));
37+
}
3438
}
3539

3640
/**

inc/templates/dashboard.php

Lines changed: 0 additions & 74 deletions
This file was deleted.

logs/index.php

Whitespace-only changes.

0 commit comments

Comments
 (0)