tryCatchLog icon indicating copy to clipboard operation
tryCatchLog copied to clipboard

Ignore selected warnings (and other conditions?)

Open aryoda opened this issue 9 years ago • 4 comments

Add the possibility to disable the logging and printing of selected warnings.

Use cases:

  • An R script produces well know warnings that shall not pollute the log file.

Implementation hints:

  • Use a global option with a vector of strings (or regular expressions?). This option is the default value for a new argument in tryCatchLog(). If the warning message contains any of these strings the warning will be dismissed.

  • Alternative implementation: Add a function suppress.selected.warning() and use it internally. Disadvantage: Difficult to overwrite in tryCatchLog() calls.

Links:

  • http://romainfrancois.blog.free.fr/index.php?post/2009/05/20/Disable-specific-warnings
  • ! https://stackoverflow.com/questions/16517795/selective-suppresswarnings-that-filters-by-regular-expression/16521046#16521046
  • Warning messages may be translated: https://stackoverflow.com/a/16121409/4468078
  • Translations in R: https://stackoverflow.com/questions/29093673/how-to-translate-package-content
  • gettext() can be used to translate texts, but how? The MsgID is not known anymore once you get a translated message text since printf arguments are injected normally... (e. g. MsgID "In log(%s): NaNs produced" -> warning message for log(-1) is "In log(-1): NaNs produced"

Localisation of messages in R is documented here: https://cran.r-project.org/doc/manuals/r-patched/R-admin.html#Locales https://cran.r-project.org/doc/manuals/r-patched/R-admin.html#Localization-of-messages

The preferred language for messages is by default taken from the locale. This can be overridden first by the setting of the environment variable LANGUAGE and then by the environment variables LC_ALL, LC_MESSAGES and LANG.

To find all possible translations of an error message you can use this solution: https://stackoverflow.com/questions/49094581/identify-actual-message-language-settings-in-r

Other posts regarding message localization: https://stackoverflow.com/questions/12642651/in-r-how-to-get-error-messages-in-english https://stackoverflow.com/questions/49094581/identify-actual-message-language-settings-in-r https://stackoverflow.com/questions/19111956/suppress-error-message-in-r

See also how the gettext toolchain is working to translate text strings contained in source code:

  • https://en.wikipedia.org/wiki/Gettext

aryoda avatar Nov 27 '16 13:11 aryoda

Currently there is no reliable way in R to recognize specific conditions:

https://stackoverflow.com/q/52935066/4468078

Next step: Ask for advice at the r-devel mailing list (current status...)

aryoda avatar Nov 16 '18 20:11 aryoda

Related to #22 (where selected conditions must be recognized, repackaged and thrown as custom conditions - this also requires a reliable way in R to recognize specific conditions)

aryoda avatar Apr 13 '20 08:04 aryoda

Related to #71: The config could be extended with another column of a keywords-whitelist (or even regexp list) per condition class row to specify which conditions shall not be logged.

Currently the config does only allow to unconditionally (!) enable and disable logging for a condition class vis "write.to.log"!

The language translation of condition messages should not be required since the executor of a script knows quite good the configured language of the execution environment. So the configuration must just be adjusted accordingly.

aryoda avatar Jul 31 '22 16:07 aryoda

Great news:

Adding a keyword list for conditions to be ignored could work now independently of language settings by "resetting" the language of the current R session to the language of the keywords (must be done by the user or somehow configured):

  1. With Sys.setLanguage() in R (>= 4.2.0) in R it is now possible to change the language more than once in the current R session by clearing the translation string cache: https://bugs.r-project.org/show_bug.cgi?id=18055

  2. An alternative implementation to change the language without side effects is implemented in withr::with_language() (calling withr::local_language()). The implementation also contains a lot background information what must be done why to change the language https://github.com/r-lib/withr/blob/main/R/language.R

    This new functions

aryoda avatar Jun 27 '23 07:06 aryoda