Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dev/core#1943 add functionality to add civicrm log into Drupal access log #18115

Merged
merged 3 commits into from
Aug 10, 2020

Conversation

sunilpawar
Copy link
Contributor

Overview

Currently We have Setting to log CiviCRM Error Log to Drupal Access Log, but functionality in code is not present.

Current behaviour

Code not present of Functionality present at /civicrm/admin/setting/debug?reset=1, Enable Drupal Watchdog Logging

Proposed behaviour

After changes, it will start logging into Drupal log if Enable Drupal Watchdog Logging is enabled

https://lab.civicrm.org/dev/core/-/issues/1943

@civibot
Copy link

civibot bot commented Aug 10, 2020

(Standard links)

@civibot civibot bot added the master label Aug 10, 2020
@@ -586,6 +586,9 @@ public static function debug_log_message($message, $out = FALSE, $prefix = '', $
if ($config->userSystem->is_drupal and function_exists('watchdog')) {
watchdog('civicrm', '%message', ['%message' => $message], $priority ?? WATCHDOG_DEBUG);
}
elseif ($config->userSystem->is_drupal and CIVICRM_UF == 'Drupal8') {
\Drupal::logger('civicrm')->debug($message);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the patch. This would always log with priority "debug". I think it should use the log function like \Drupal::logger('civicrm')->log($priority ?? \Drupal\Core\Logger\RfcLogLevel::DEBUG, '%message', ['%message' => $message])

The '%' part is just to make it the same as drupal 7. Could also be \Drupal::logger('civicrm')->log($priority ?? \Drupal\Core\Logger\RfcLogLevel::DEBUG, $message, []).

I see the note on line 585. I'm not sure what the situation it talks about is but I guess it means you can't put this in the UF class instead so yes it has to go here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks @demeritcowboy , updated patch..

@demeritcowboy
Copy link
Contributor

  • General standards
    • [r-explain] PASS
      • Minor note: It's not "access logs" which is something else in drupal but it's clear what it means later (/admin/reports/dblog).
    • [r-user] PASS
    • [r-doc] PASS
    • [r-run] PASS
      • Works like in drupal 7 now.
  • Developer standards
    • [r-tech] PASS
      • As noted it would be better to put these in the UF classes but the comment on line 585 says that sometimes breaks.
      • Out of scope and I haven't tested or investigated but for calls that go through \Civi::log() instead of calling debug_log_message() directly, you shouldn't need this since you should be able to add any other PSR logger (e.g. drupal's logger) to the logging listeners via config.
    • [r-code] PASS
    • [r-maint] N/A
      • Difficult to write a test.
    • [r-test] Pending
      • Tests are still running.

@seamuslee001 seamuslee001 merged commit 677cebc into civicrm:master Aug 10, 2020
@seamuslee001
Copy link
Contributor

Thanks @demeritcowboy @sunilpawar

@@ -586,6 +586,9 @@ public static function debug_log_message($message, $out = FALSE, $prefix = '', $
if ($config->userSystem->is_drupal and function_exists('watchdog')) {
watchdog('civicrm', '%message', ['%message' => $message], $priority ?? WATCHDOG_DEBUG);
}
elseif ($config->userSystem->is_drupal and CIVICRM_UF == 'Drupal8') {
\Drupal::logger('civicrm')->log($priority ?? \Drupal\Core\Logger\RfcLogLevel::DEBUG, '%message', ['%message' => $message]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sunilpawar @demeritcowboy:

(1) Are y'all sure that the pass-thru of $priority is right here? The thing is that PSR-3 and PEAR have different constants for log-levels/log-priorities. (PSR-3 uses strings; PEAR uses ints.) Observe that:

  • In CRM_Core_Error_Log, it uses an array-map to ensure that it gives a PEAR-style value.
  • Line 568 above passes $priority into a PEAR logger... so it had better be a PEAR-style value.
  • The docblock therefore seems wrong - ie $prioritity is numeric. (The docblock inLog_file::log() is also wrong... but if you drill-down to Log_file::log() => Log::_format() => Log::priorityToString(), it really does expect PEAR's numeric constants.)

I'm pretty sure this would need to reverse the mapping.

(2) There is a pre-existing issue (not the fault of this PR, but relevant to note) that the relationship between CRM_Core_Error_Log (Civi's PSR-3 stub) and CRM_Core_Error::debug_log_message() (Civi's PEAR wrapper) is bit off - IMHO, it's backwards. The relevance is that the current chain (CRM_Core_Error_Log => CRM_Core_Error => D8 Log) produces lossy a conversion. Without the structure of $message and $context, it'll be impossible to have something in the D8 realm to Nice Things (like pretty-printing, categorizing, translating). If the chain were setup better, then it wouldn't lose anything.

(Again, that's not the fault of this PR...)

@demeritcowboy
Copy link
Contributor

demeritcowboy commented Aug 13, 2020

It worked when I tested it with different errors. But you made me look and it turns out the DbLog class which is the implementation that ends up getting used doesn't use PSR levels, it uses RFC levels, which are ints. RfcLoggerTrait and DbLog. You know that saying about the great thing about standards...

@totten
Copy link
Member

totten commented Aug 13, 2020

Aaaah, you're right. That's wild!

I guess it works because RfcLogLevels numerically match the PEAR levels. (Which makes sense - they're both following RFC-5424's numbers.)

I had some trouble believing that DbLog would break PSR-3 compliance by requiring ints. It turns out that we've got middle-man here: Drupal::logger() returns an instance of LoggerChannel, which has a normalization mechanism and accepts either the ints or the strings. Then it gives a normalized int to DbLog.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants