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

Add logging_util and rotating log archive for uiserver logs #376

Merged
merged 7 commits into from
Aug 19, 2022

Conversation

datamel
Copy link
Contributor

@datamel datamel commented Aug 11, 2022

Logs for the uiserver can now be found in ~/.cylc/uiserver/log

These changes close #352

When a new instance is started, the previous log is archived. The archive will retain the last 5 logs.
For convenience, and to keep functionality similar to cylc flow, a log symlink, linking to the latest log has been added.

Requirements check-list

  • I have read CONTRIBUTING.md and added my name as a Code Contributor.
  • Contains logically grouped changes (else tidy your branch by rebase).
  • Does not contain off-topic changes (use other PRs for other changes).
  • Any dependency changes applied to setup.cfg.
  • Appropriate tests are included (unit and/or functional).
  • Appropriate change log entry included.
  • I have opened a documentation PR at cylc/cylc-doc/pull/XXXX.
  • No documentation update required.

@datamel datamel added this to the 1.2.0 milestone Aug 11, 2022
@datamel datamel self-assigned this Aug 11, 2022
@codecov-commenter
Copy link

codecov-commenter commented Aug 11, 2022

Codecov Report

Merging #376 (e95e263) into master (fe49e35) will increase coverage by 0.56%.
The diff coverage is 95.12%.

@@            Coverage Diff             @@
##           master     #376      +/-   ##
==========================================
+ Coverage   79.15%   79.72%   +0.56%     
==========================================
  Files          11       12       +1     
  Lines        1118     1159      +41     
  Branches      215      219       +4     
==========================================
+ Hits          885      924      +39     
- Misses        195      197       +2     
  Partials       38       38              
Impacted Files Coverage Δ
cylc/uiserver/__init__.py 87.50% <50.00%> (-12.50%) ⬇️
cylc/uiserver/logging_util.py 100.00% <100.00%> (ø)
cylc/uiserver/scripts/gui.py 100.00% <100.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

Copy link
Member

@oliver-sanders oliver-sanders left a comment

Choose a reason for hiding this comment

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

Tested, works well.

@oliver-sanders
Copy link
Member

Nice, tested with single & multi user modes, rollover worked for both even when interleaved.

Comment on lines 51 to 53
"""Increment the log number by one for each log"""
# increments each log number, done in descending order to avoid
# filename conflicts
Copy link
Member

Choose a reason for hiding this comment

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

style suggestion

Suggested change
"""Increment the log number by one for each log"""
# increments each log number, done in descending order to avoid
# filename conflicts
"""Increment the log number by one for each log.
Increments each log number, done in descending order to avoid
filename conflicts.
"""

self.file_path, f"*{self.LOG_NAME_EXTENSION}")), reverse=True)
for file in log_files:
log_num = int(Path(file).name.partition('-')[0]) + 1
new_file_name = Path(
Copy link
Member

Choose a reason for hiding this comment

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

Why can't you just

if log_num > 5:
    file.unlink()

And never create the older archive members?

@@ -24,7 +24,7 @@

from cylc.uiserver import (
__version__,
__file__ as uis_pkg
__file__ as uis_pkg,
Copy link
Member

Choose a reason for hiding this comment

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

Any particular reason?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, I initially added init_log to this import. I'll remove.

Copy link
Member

@oliver-sanders oliver-sanders Aug 18, 2022

Choose a reason for hiding this comment

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

FYI: trailing commas [perfectly valid] are a signal to black to leave room for further additions so can be considered functional. Although we don't use black I use trailing commas as a matter of course.

Copy link
Member

Choose a reason for hiding this comment

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

So do I; it was more that I was curious that this had been changed at all for this PR.

Comment on lines +30 to +38
for file in [
'03-uiserver.log',
'01-uiserver.log',
'04-uiserver.log',
'02-uiserver.log',
'05-uiserver.log',
'07-uiserver.log',
'06-uiserver.log'
]:
Copy link
Member

@wxtim wxtim Aug 17, 2022

Choose a reason for hiding this comment

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

You could make these tests a bit smaller if you wished to:

Suggested change
for file in [
'03-uiserver.log',
'01-uiserver.log',
'04-uiserver.log',
'02-uiserver.log',
'05-uiserver.log',
'07-uiserver.log',
'06-uiserver.log'
]:
for file in [f'{i:d02}-uiserver.log' for i in range(1,7)]

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was making them intentionally out of numerical order.

]:
log_file = LOG.file_path.joinpath(file)
log_file.touch()
log_file.write_text(f"File: {file}")
Copy link
Member

Choose a reason for hiding this comment

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

I find this easier to understand?

Suggested change
log_file.write_text(f"File: {file}")
log_file.write_text(f"This file started as: {file}")

f'{LOG.file_path}/03-uiserver.log',
f'{LOG.file_path}/02-uiserver.log']
assert sorted(log_files) == sorted(expected_files)
actual_output = Path(LOG.file_path/'03-uiserver.log').read_text() # LOG.file_path/{03-uiserver.log}
Copy link
Member

Choose a reason for hiding this comment

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

What's the comment for?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll remove.

log_files = sorted(glob(os.path.join(
self.file_path, f"*{self.LOG_NAME_EXTENSION}")), reverse=True)
for file in log_files:
log_num = int(Path(file).name.partition('-')[0]) + 1
Copy link
Member

@wxtim wxtim Aug 17, 2022

Choose a reason for hiding this comment

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

You might want to consider using a try/except loop around this, or making the glob a bit more careful ([0-9]*{self.LOG_NAME_EXTENSION}).

Otherwise I can break this thus:

touch ~/.cylc/uiserver/log/iduhafiuhbjvfaiehr-uiserver.log

Copy link
Member

Choose a reason for hiding this comment

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

Good spot, unlikely but worth fixing.

Copy link
Member

@wxtim wxtim left a comment

Choose a reason for hiding this comment

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

It looks like it's working, apart from my destructive testing where I added fkjaiuj-uiserver.log to the log directory.

I've made a few suggestions of the mild/moderate sort.

@datamel
Copy link
Contributor Author

datamel commented Aug 18, 2022

@wxtim I've done a small refactor to address your comments.

@datamel datamel requested a review from wxtim August 18, 2022 18:51
@wxtim wxtim merged commit 549afbd into cylc:master Aug 19, 2022
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.

logging: rollover on restart
4 participants