Skip to content

Configurable logging #3730

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

Open
wants to merge 4 commits into
base: integration
Choose a base branch
from

Conversation

Ryszard-Trojnacki
Copy link

Added possibility to configure LiteFarm logging.

Till now all logging configuration was hardcoded and only variable was NODE_ENV. I have introduced new variables for logging:

  • LOG_LEVEL - allows to setup main/file logging,
  • LOG_CONSOLE_LEVEL - allows to setup console login,
  • LOG_DISABLE_SENTRY - allows to disable Sentry logging.

Also I have changed console logging format from JSON to cli, because it was totally unreadable.

Beside changing the level of logging, there is a possibility to turn off logging by setting off value to parameter. LOG_CONSOLE_LEVEL=off allows to turn off console logging.

New variables does not change the actual behavior.
Only change that is done by this patch is change of default format of console logging.

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

I have updated .env.default file with variables.

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

  • Passes test case
  • UI components visually reviewed on desktop view
  • UI components visually reviewed on mobile view
  • Other (please explain)

Tested all configuration options manually.

Checklist:

  • I have commented my code, particularly in hard-to-understand areas
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • The precommit and linting ran successfully
  • I have added or updated language tags for text that's part of the UI
  • I have added "MISSING" for all new language tags to languages I don't speak
  • I have added the GNU General Public License to all new files

Changed format of console logging.
Added ability to turn off logging.
Added option to turn off Sentry logging.
@Ryszard-Trojnacki Ryszard-Trojnacki requested review from a team as code owners April 3, 2025 14:40
@Ryszard-Trojnacki Ryszard-Trojnacki requested review from antsgar and SayakaOno and removed request for a team April 3, 2025 14:40
* @param value log level string
* @param def default log level
*/
function getLogLevel(value, def) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could we change def to default? I prefer not to shorten variable names because it can be confusing

Copy link
Author

Choose a reason for hiding this comment

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

default is a JavaScript reserved keyword used in switch. It can't be used as a variable name.

Copy link
Author

Choose a reason for hiding this comment

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

OK, I have changed this to defaultValue. I hope this is OK now.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ah, you're right! Ironically I think I found def confusing from how it's used in Python. Thanks for updating it!

Comment on lines 22 to 23
if (typeof value !== 'string') return def;
value = value.toLowerCase().trim();
Copy link
Collaborator

Choose a reason for hiding this comment

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

NIT but we usually prefer to avoid inline conditionals

Copy link
Author

Choose a reason for hiding this comment

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

Done and corrected in other places.

@@ -61,7 +101,7 @@ class SentryTransport extends Transport {
}

// Report Errors to Sentry
if (process.env.NODE_ENV !== 'development') {
if (process.env.NODE_ENV !== 'development' && !isTrue(process.env.LOG_DISABLE_SENTRY)) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

The negation of isTrue is a bit hard to read IMO. Could we change the env var to LOG_ENABLE_SENTRY instead?

Copy link
Author

Choose a reason for hiding this comment

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

Done.

@antsgar
Copy link
Collaborator

antsgar commented Apr 8, 2025

@Ryszard-Trojnacki left a few minor comments, thank you for working on this!

@Ryszard-Trojnacki
Copy link
Author

@antsgar I have included all your comments.

antsgar
antsgar previously approved these changes Apr 8, 2025
Co-authored-by: Antonella Sgarlatta <[email protected]>
antsgar
antsgar previously approved these changes Apr 11, 2025
if (process.env.NODE_ENV !== 'production') {
const consoleLogLevel = getLogLevel(
process.env.LOG_CONSOLE_LEVEL,
process.env.NODE_ENV !== 'production' ? 'error' : 'off',
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm so sorry for the late review Ryszard.

@antsgar
As we discussed in this Slack thread, we want to enable error logs in production. Could you confirm that:

process.env.NODE_ENV !== 'production' ? 'error' : 'off'

should instead just be 'error'?

Just checking before requesting any changes, especially since this PR has been open for a while.

Also, if we go ahead with this change, we would need to replace console.log used for error logging with console.error.

Copy link
Author

Choose a reason for hiding this comment

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

I have changed the first part.

Also, if we go ahead with this change, we would need to replace console.log used for error logging with console.error.

As I understand you want me to change almost all calls to console.log to console.error?

There is also other option. I can change:

console.log = (...args) => logger.info.call(logger, ...args);

to:

console.log = (...args) => logger.error.call(logger, ...args);

and console.log would be by default logger.error.

Copy link
Collaborator

Choose a reason for hiding this comment

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

@Ryszard-Trojnacki Thank you so much for making the change and for your comment!
We actually discussed it this morning and are now thinking about setting LOG_CONSOLE_LEVEL to info, since we’ll want to support both info and log.

I also noticed that objects are being logged as undefined with the changes. I was thinking we might want to consider formatting the output using winston.format.printf (https://github.com/winstonjs/winston?tab=readme-ov-file#formats).
Do you happen to have any suggestions or alternatives that might work better?

Just for reference, here's the current log:
Screenshot 2025-05-14 at 11 43 35 AM

And here's how it looks with the changes:
Screenshot 2025-05-14 at 11 54 47 AM

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

Successfully merging this pull request may close these issues.

3 participants