Skip to content

Commit 5ab8e56

Browse files
feat: allow configurable log levels
1 parent 3bfb6b1 commit 5ab8e56

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

pfSense-pkg-RESTAPI/files/usr/local/pkg/RESTAPI/Core/BaseTraits.inc

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace RESTAPI\Core;
44

55
use ReflectionClass;
6+
use RESTAPI\Models\RESTAPISettings;
67
use function RESTAPI\Core\Tools\get_classes_from_namespace;
78

89
/**
@@ -42,8 +43,11 @@ trait BaseTraits {
4243
* info.xml file.
4344
*/
4445
public function log(int $level, string $message, string $logfile = 'restapi'): void {
45-
# Only log debug messages when verbose logging is enabled
46-
if ($level === LOG_DEBUG and !$this->config->debug_mode) {
46+
# Get the log level limit from the RESTAPI settings
47+
$log_limit = (int) RESTAPISettings::get_pkg_config()['log_level'] ?? 3;
48+
49+
# Do not log if the incoming level is higher than the configured log level
50+
if ($level >= $log_limit) {
4751
return;
4852
}
4953

pfSense-pkg-RESTAPI/files/usr/local/pkg/RESTAPI/Models/RESTAPISettings.inc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class RESTAPISettings extends Model {
3131
public BooleanField $keep_backup;
3232
public BooleanField $login_protection;
3333
public BooleanField $log_successful_auth;
34+
public IntegerField $log_level;
3435
public BooleanField $allow_pre_releases;
3536
public BooleanField $hateoas;
3637
public BooleanField $expose_sensitive_fields;
@@ -93,6 +94,22 @@ class RESTAPISettings extends Model {
9394
failed API authentication attempts are logged to prevent flooding the authentication logs. This field is
9495
only applicable when the API `login_protection` setting is enabled.",
9596
);
97+
$this->log_level = new IntegerField(
98+
default: 3,
99+
choices: [
100+
0 => '0 - Emergency',
101+
1 => '1 - Alert',
102+
2 => '2 - Critical',
103+
3 => '3 - Error',
104+
4 => '4 - Warning',
105+
5 => '5 - Notice',
106+
6 => '6 - Info',
107+
7 => '7 - Debug',
108+
],
109+
verbose_name: 'log level',
110+
help_text: 'Sets the log level for API logging. The log level determines the severity of messages that are
111+
logged. Setting a higher log level will include all messages of that level and lower severity.',
112+
);
96113
$this->allow_pre_releases = new BooleanField(
97114
default: false,
98115
indicates_true: 'enabled',

pfSense-pkg-RESTAPI/files/usr/local/share/pfSense-pkg-RESTAPI/info.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
<keep_backup>enabled</keep_backup>
2121
<login_protection>enabled</login_protection>
2222
<log_successful_auth>disabled</log_successful_auth>
23+
<log_level>3</log_level>
2324
<hateoas>disabled</hateoas>
2425
<expose_sensitive_fields>disabled</expose_sensitive_fields>
2526
<override_sensitive_fields></override_sensitive_fields>

0 commit comments

Comments
 (0)