Skip to content

Shift to winston for logging#229

Merged
confluence merged 26 commits into
devfrom
aikema/winston_logging
Jun 13, 2025
Merged

Shift to winston for logging#229
confluence merged 26 commits into
devfrom
aikema/winston_logging

Conversation

@daikema
Copy link
Copy Markdown
Contributor

@daikema daikema commented May 20, 2025

This pull request migrates from controller logging using console methods (and occasionally util functions verboseError or verboseLog), to controller logging using winston.

As part of this migration the verbose parameter has been replaced with the option to specify a logLevel, where any messages of or above that level will be logged. Rather than only logging to the console, with the changes log level and log format (text vs json) can be independently specified for both console and file.

Text log example (log level is colorized when run):

2025-05-20T15:00:12.593Z [INFO]: Started logging to /tmp/carta-json.log
2025-05-20T15:00:12.595Z [INFO]: Loaded config from /etc/carta/config.json
2025-05-20T15:00:13.903Z [INFO]: Using /etc/carta/userlookupOidc.txt for user mapping
2025-05-20T15:00:13.904Z [INFO]: Updated usermap with 3 entries
2025-05-20T15:00:14.534Z [INFO]: Serving packaged CARTA frontend (Version 5.0.0-beta.1c)
2025-05-20T15:00:14.576Z [INFO]: Setting up JWKS management for https://auth.server.name/realms/default/protocol/openid-connect/certs
2025-05-20T15:00:14.594Z [INFO]: Connected to test on localhost:27017 (Authenticated: No)
2025-05-20T15:00:14.595Z [INFO]: Server initialised successfully at https://server.name/
2025-05-20T15:00:14.596Z [INFO]: Started listening for login requests on port 8000

JSON log example:

{"level":"info","message":"Started logging to /tmp/carta-json.log","timestamp":"2025-05-20T15:00:12.595Z"}
{"level":"info","message":"Loaded config from /etc/carta/config.json","timestamp":"2025-05-20T15:00:12.595Z"}
{"level":"info","message":"Using /etc/carta/userlookupOidc.txt for user mapping","timestamp":"2025-05-20T15:00:13.904Z"}
{"level":"info","message":"Updated usermap with 3 entries","timestamp":"2025-05-20T15:00:13.904Z"}
{"level":"info","message":"Serving packaged CARTA frontend (Version 5.0.0-beta.1c)","timestamp":"2025-05-20T15:00:14.534Z"}
{"level":"info","message":"Setting up JWKS management for https://auth.server.name/realms/default/protocol/openid-connect/certs","timestamp":"2025-05-20T15:00:14.576Z"}
{"level":"info","message":"Connected to test on localhost:27017 (Authenticated: No)","timestamp":"2025-05-20T15:00:14.594Z"}
{"level":"info","message":"Server initialised successfully at https://server.name/","timestamp":"2025-05-20T15:00:14.595Z"}
{"level":"info","message":"Started listening for login requests on port 8000","timestamp":"2025-05-20T15:00:14.597Z"}

Still TBD

  • Documentation updates outside the schema JSON + CLI --help (should be done before converting from a draft pull request)
  • Creation of a corresponding pull request to the new schemas repo

Questions / Tentative answers

  • Should I keep the console logging colorized even if writing to something like a file? (less gets confused by the colorization but cat is happy with it). Now only colorizing output to console.
  • Do we want to support more log destinations other than just console + file? (There's quite a few possibilities that could be integrated without too much work, but which would drag in more dependencies). Tentatively keeping to just console + file pending further modularization of controller.
  • Should we assume that the user is responsible for logrotation using tools of their choice, or build support for this directly into the tool using some of the existing transport options there for that? (Note that I haven't yet confirmed that 3rd party logrotation tools don't conflict). Should include test logrotation with logrotate.d
  • Is it best to keep logs in UTC like here at the moment, or in the server's local time zone? Should default to local timezone (with possible option for a specified alternate timezone)

@confluence
Copy link
Copy Markdown
Collaborator

* Should I keep the console logging colorized even if writing to something like a file?  (`less` gets confused by the colorization but `cat` is happy with it).

I vote no; it's going to cause problems. It is nice in the console, though.

* Do we want to support more log destinations other than just console + file?  (There's [quite a few possibilities](https://github.com/winstonjs/winston/blob/master/docs/transports.md#winston-transports) that could be integrated without too much work, but which would drag in more dependencies).

I don't think we should bother unless someone explicitly requests it.

* Should we assume that the user is responsible for logrotation using tools of their choice, or build support for this directly into the tool using some of the existing transport options there for that?  (Note that I haven't yet confirmed that 3rd party logrotation tools don't conflict).

I vote yes (to assuming that the user will rotate logs with an external tool). We're already relying on an external tool to create a system service, and this task belongs at the same level.

* Is it best to keep logs in UTC like here at the moment, or in the server's local time zone?

I very strongly vote for the server's local time zone. That's the norm for system logs, and it's pretty frustrating for the CARTA logs to be off by two hours from e.g. the nginx logs.

@veggiesaurus
Copy link
Copy Markdown
Collaborator

veggiesaurus commented May 20, 2025

I vote yes (to assuming that the user will rotate logs with an external tool). We're already relying on an external tool to create a system service, and this task belongs at the same level.

Agreed. Log rotation is definitely outside of the scope of the log source.

I very strongly vote for the server's local time zone. That's the norm for system logs, and it's pretty frustrating for the CARTA logs to be off by two hours from e.g. the nginx logs.

Yes, I agree, provided we ensure we indicate the timezone in the timestamp :)

I don't think we should bother unless someone explicitly requests it.

In this case, someone is asking for it: @robsimmonds wants it to be connectable to grafana, which should work with winston-loki, or perhaps we can get it to just work with the builtin winston HTTP transport module 🤞🏻

Happy for those to come later though

@daikema
Copy link
Copy Markdown
Contributor Author

daikema commented May 21, 2025

I don't think we should bother unless someone explicitly requests it.

In this case, someone is asking for it: @robsimmonds wants it to be connectable to grafana, which should work with winston-loki, or perhaps we can get it to just work with the builtin winston HTTP transport module 🤞🏻

I'd gotten the impression that @robsimmonds wanted us to be able support logging to a network endpoint, but wasn't 100% settled on the specific technology. Given that winston supports quite a few options in that regard (and winston-loki doesn't even make the main transports list), I'm leaning towards leaning towards deferring that to later - possibly adding logging to the list of things to be made pluginable.

@daikema
Copy link
Copy Markdown
Contributor Author

daikema commented Jun 11, 2025

OK. So this now should be ready for review.

Doc updates are done including an example config for log rotation. Should note that as part of this change I've gone and renamed LogFileTemplate to backendLogFileTemplate which would be a breaking change for those using this option in this their configs as the original name was getting confusing with the controller now having it's own logging options. (Should I make this backwards-compatible, or since this would likely only be encountered by users as part of a new major release would it be OK to leave as is?).

Pull request to the schemas repos with similar updates there I figured I'd save for after some comments on the schema changes already done here to avoid having to revise in a couple of places at same time.

@daikema daikema marked this pull request as ready for review June 11, 2025 11:24
@daikema
Copy link
Copy Markdown
Contributor Author

daikema commented Jun 11, 2025

Should maybe also mention that I'm using syslog log levels instead of npm log levels (even though npm log levels are default). If you'd prefer to stick to defaults it should be pretty straightforward to switch.

@daikema
Copy link
Copy Markdown
Contributor Author

daikema commented Jun 11, 2025

@veggiesaurus realized that you'd mentioned that "provided we ensure we indicate the timezone in the timestamp :)"

Should note that the updated version logs in the server's local timezone by default but doesn't indicate the timezone in the output. It's also possible to override the timezone in the config file, but if you override the timezone the updated timezone also isn't indicated in the timestamp. Not sure if you want the timezone in place in such cases.

Comment thread config/config_schema.json Outdated
Comment thread config/config_schema.json Outdated
Comment on lines +750 to +754
"logFile": {
"description": "Name of file to send log messages to.",
"type": "string",
"examples": ["/var/log/carta-controller.log", "/var/spool/carta/controller.log"]
},
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This isn't required and has no default. What happens if it's not specified? If that disables logging to a file, the description should mention it.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Since the backend log file location does have a default, I think it would be good for this to have one as well.

Since the default backend file location is /var/log/carta/username..., I don't love making the default /var/log/carta/controller.log, but it would be nice to keep everything in /var/log/carta/, and none of the alternatives are appealing.

Also, the description should mention that no log rotation is performed by the controller and must be configured separately. Got to the docs; maybe this isn't necessary.

I still think logging to file should be enabled by default, although if rotation is not set up by default perhaps we should discuss this further. I'm concerned that users won't have logging available by default unless they set something up, but having a single file that keeps growing by default is also not good.

Copy link
Copy Markdown
Contributor Author

@daikema daikema Jun 12, 2025

Choose a reason for hiding this comment

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

Wonder if @veggiesaurus has any thoughts here.

Thus far the controller has solely logged to the console, leaving the remainder up to the user. Here I added "logFile":"/var/log/carta/controller.log", to the example config to emphasize that logging might be useful but otherwise in this respect it behaves similar to prior to the changes in this PR.

But you are right @confluence that the backend logs do have a default, so adding a new default here wouldn't be super-weird or lacking precedent. Had to add a su carta carta to the logrotate example given log file ownership, so figure we sorta want it separated into a directory of its own for log rotation to work nicely. It doesn't sound like you've got a better idea though?

having a single file that keeps growing by default is also not good.

To be fair, at the moment we don't include any example of how to clean up old backend logs so if you have a server that's been running for some time you might end up with a large number of files there. Perhaps we should also add an example cron job that clears backend logs that've been around for, e.g., over 30 days from there?

Comment thread config/config_schema.json Outdated
},
"logFileTemplate": {
"description": "Location of log file. The `{username}`, `{pid}` and `{datetime}` placeholders will be replaced with the username, process ID. and dat/time formatted as `YYYYMMDD.h_mm_ss` respectively",
"backendLogFileTemplate": {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

We can't rename this without bumping the schema version and adding functionality for dealing with older config files (it's a non-backwards-compatible change).

I suggest that we just change the description for now (since there's no name clash), and in a separate issue figure out how to deprecate the old name correctly. This would probably require adding the handling of the fallback name plus a deprecation warning.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Created #234.

Also pushed changes that revert the logFileTemplate -> backendLogFileTemplate for now to make merge-ready.

Addition of backwards compatibility code as I'd suggested here is something that could be done quickly. Still tempted to get the change + backwards compatibility in before the merge as a new major release would seem the ideal time to make something deprecated IMO (@confluence? Would basically need to add a new deprecated schema entry + then a tiny bit of compatibility code to throw a deprecation warning).

"killCommand": "/usr/bin/carta-kill-script",
"rootFolderTemplate": "/home/{username}",
"baseFolderTemplate": "/home/{username}",
"logFile":"/var/log/carta/controller.log",
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

If we add a default, we should remove this.

Comment thread docs/src/_static/output/alicetest.txt Outdated
Comment thread docs/src/_static/output/alicetest.txt Outdated
Comment thread docs/src/configuration.rst Outdated
@daikema
Copy link
Copy Markdown
Contributor Author

daikema commented Jun 12, 2025

This is now hopefully ready to merge just after CARTAvis/schemas#7 assuming no further change requests (and if I can figure out the git incantations related to submodules).

@daikema daikema requested a review from confluence June 12, 2025 19:12
Copy link
Copy Markdown
Collaborator

@confluence confluence left a comment

Choose a reason for hiding this comment

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

Minor request for message wording change; otherwise looks good. Please remember to bump the schemas submodule version again after the schemas PR has been merged.

Comment thread src/config.ts Outdated
@confluence confluence merged commit fbd9e3b into dev Jun 13, 2025
@confluence confluence deleted the aikema/winston_logging branch June 13, 2025 10:39
daikema added a commit that referenced this pull request Jun 27, 2025
* Documentation updates for v5 release (#202)

* Preemptively add tabs extension for later use

* Update Ubuntu versions; update MongoDB instructions; update minimum Node version

* Also update node version in AlmaLinux instructions.

* Major overhaul of step-by-step instructions. Removed RHEL/CentOS 7 instructions. Expanded some details and made DEB and RPM instructions more similar. Not yet tested or complete.

* Expanded last sections and removed trailing whitespace for easier diff of instructions

* Switched back to packaged versions of certbot to avoid snap; reworded notes

* More details in controller config; some rewording and tweaks

* Merged step-by-step instructions into a single document, and revised some general information.

* tweaked headings

* Expanded backend installation section

* Added executable location.

* Added more information about backend packages to installation section

* Added note about backend.json permissions

* Filled in more of the installation section

* Reworded and expanded info about domain + certs

* Rephrased 'local installation' of node.js

* Fixed multiple issues found during test of step-by-step instructions (incomplete). Standardised to NodeSource instructions for all platforms, to ensure consistent default executable location.

* mentioned custom paths in more places in installation

* tweak

* Made a variety of corrections and changes based on tests on Ubuntu and AlmaLinux

* Added commented-out section about debugging symbols for RPM packages

* pass to remove trailing whitespace from docs

* changed npm run start to npm start

* a word

* Fixed heading shortcuts in step-by-step

* Fixed link

* Added commands for installing vim and curl

* Added literal packages line to c&p for AlmaLinux8

* Add update-alternatives command for multiple installed Python versions on AlmaLinux 8

* more explicit instructions for nonstandard executable location in beta rpm package

* clearer wording about custom paths

* fixed badges for dev

* Update frontend to fix rounding

* Clarified on the main page that 24.04 is only supported in v5.x (#215)

* Create the global casacore data directory in the sample update script. (#216)

* Bump axios from 0.28.1 to 1.8.2 (#218)

Bumps [axios](https://github.com/axios/axios) from 0.28.1 to 1.8.2.
- [Release notes](https://github.com/axios/axios/releases)
- [Changelog](https://github.com/axios/axios/blob/v1.x/CHANGELOG.md)
- [Commits](axios/axios@v0.28.1...v1.8.2)

---
updated-dependencies:
- dependency-name: axios
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Bump @babel/helpers from 7.26.9 to 7.26.10 (#222)

Bumps [@babel/helpers](https://github.com/babel/babel/tree/HEAD/packages/babel-helpers) from 7.26.9 to 7.26.10.
- [Release notes](https://github.com/babel/babel/releases)
- [Changelog](https://github.com/babel/babel/blob/main/CHANGELOG.md)
- [Commits](https://github.com/babel/babel/commits/v7.26.10/packages/babel-helpers)

---
updated-dependencies:
- dependency-name: "@babel/helpers"
  dependency-type: indirect
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Bump @babel/runtime from 7.26.9 to 7.26.10 (#223)

Bumps [@babel/runtime](https://github.com/babel/babel/tree/HEAD/packages/babel-runtime) from 7.26.9 to 7.26.10.
- [Release notes](https://github.com/babel/babel/releases)
- [Changelog](https://github.com/babel/babel/blob/main/CHANGELOG.md)
- [Commits](https://github.com/babel/babel/commits/v7.26.10/packages/babel-runtime)

---
updated-dependencies:
- dependency-name: "@babel/runtime"
  dependency-type: indirect
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Dynamically determine controller API location in client-side Javascript (#226)

* Dynamically determine controller API location in client-side Javascript

* Revert changes to api mounts (#227)

* Bump http-proxy-middleware from 2.0.7 to 2.0.9 (#228)

Bumps [http-proxy-middleware](https://github.com/chimurai/http-proxy-middleware) from 2.0.7 to 2.0.9.
- [Release notes](https://github.com/chimurai/http-proxy-middleware/releases)
- [Changelog](https://github.com/chimurai/http-proxy-middleware/blob/v2.0.9/CHANGELOG.md)
- [Commits](chimurai/http-proxy-middleware@v2.0.7...v2.0.9)

---
updated-dependencies:
- dependency-name: http-proxy-middleware
  dependency-version: 2.0.9
  dependency-type: indirect
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Make parsing of user lookup table file more robust (#230)

* Make parsing of user lookup table file more robust

* Adjust trim / comment logic to avoid problems with line terminators

* Added basic unit test for user table parsing

* Run tsc before jest

* Switched from Jest to Vitest

* Made unit test file a ts file; added another test case for a bad line with a comment

* Forgot to bump version (#232)

* forgot to bump version

* determine version programmatically from package.json

* pedantic blank line removal

* Use a custom pygments lexer and extend the default sphinx pygments style to enable ANSI colours in controller test output in docs (#231)

* Schema cleanup (#221)

* Added schemas submodule

* Copied .prettierrc from frontend (additional option)

* bumped schemas after reformatting

* Changed paths to schema files; updated example controller and backend config

* removed docs from build

* also exclude RTD metadata from package

* updated submodule

* bumped schemas submodule

* fixed typo

* bumped submodule

* bumped submodule

* bumped schemas

* Union types removed from new schemas

* Warn if returning invalid object. Made existing log messages rejecting invalid object updates more verbose; turned into warnings. Corrected failure messages returned on invalid update to distinguish malformed and invalid updates.

* added missing import

* Move schemas submodule to https url

* bumped schemas

* Moved old config directory into docs, which is the only place it's currently used

* bumped schemas submodule

* Shift to winston for logging (#229)

* Progress

* Database file changes

* Still single-log-output instead of both console+file

* tslog -> winston

* Cleanup

* Migration log formatting to config; remove colorization from file log

* Update test to use winston

* Fix test log symbol output

* Update --test example with winston-based output

* Prune intro from test output

* Slightly different timestamp format now with timezone support

* Improve formatting consistency

* Update docs with example logrotate configuration

* remove linebreak

* Minor log updates

* Update controller test example output to match default locations

* Revert change of logFileTemplate to backendLogFileTemplate

* Revert "Revert change of logFileTemplate to backendLogFileTemplate"

This reverts commit 9aedc92.

* Handle logFileTemplate as deprecated, warning if defined

* Update schema repo pin

* Patch usermap parsing test

* Update schema repo after logging PR merge

* Fixing 'pedantic wording thing'

* package + -lock need version update

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: Adrianna Pińska <adrianna.pinska@gmail.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
JananShaikh pushed a commit to robsimmonds/carta-collab-controller that referenced this pull request Jul 28, 2025
* Progress

* Database file changes

* Still single-log-output instead of both console+file

* tslog -> winston

* Cleanup

* Migration log formatting to config; remove colorization from file log

* Update test to use winston

* Fix test log symbol output

* Update --test example with winston-based output

* Prune intro from test output

* Slightly different timestamp format now with timezone support

* Improve formatting consistency

* Update docs with example logrotate configuration

* remove linebreak

* Minor log updates

* Update controller test example output to match default locations

* Revert change of logFileTemplate to backendLogFileTemplate

* Revert "Revert change of logFileTemplate to backendLogFileTemplate"

This reverts commit 9aedc92.

* Handle logFileTemplate as deprecated, warning if defined

* Update schema repo pin

* Patch usermap parsing test

* Update schema repo after logging PR merge

* Fixing 'pedantic wording thing'
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