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

Feature/logging #85

Merged
merged 11 commits into from
Aug 29, 2017
Merged

Feature/logging #85

merged 11 commits into from
Aug 29, 2017

Conversation

PanSzczerba
Copy link
Contributor

Added new log method in generator.py implementing features from "better logging" issue

@grzanka
Copy link
Collaborator

grzanka commented Aug 26, 2017

@PanSzczerba why do I see my commits in the list https://github.com/DataMedSci/mcpartools/pull/85/commits ?

Is your fork and your master branch up-to-date with https://github.com/DataMedSci/mcpartools ?

@grzanka
Copy link
Collaborator

grzanka commented Aug 26, 2017

@codecov-io
Copy link

codecov-io commented Aug 26, 2017

Codecov Report

Merging #85 into master will increase coverage by 0.34%.
The diff coverage is 46.42%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #85      +/-   ##
==========================================
+ Coverage   63.98%   64.32%   +0.34%     
==========================================
  Files          12       12              
  Lines         844      855      +11     
==========================================
+ Hits          540      550      +10     
- Misses        304      305       +1
Impacted Files Coverage Δ
mcpartools/generator.py 73.49% <100%> (+1.69%) ⬆️
mcpartools/scheduler/common.py 34.61% <6.25%> (-5.39%) ⬇️
mcpartools/_version.py 43.32% <0%> (+0.36%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update e02ebcd...38ff22e. Read the comment docs.

Copy link
Collaborator

@grzanka grzanka left a comment

Choose a reason for hiding this comment

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

Before continuing, please ensure that you did the PR correctly. First two commits in this PR shouldn't be here.

@@ -111,6 +114,9 @@ def run(self):
# make symlinks to external files found
self.symlink_external_files()

# store information about command line arguments, date, time, user and hostname into generatemc.log
self.log()

# save logs
self.save_logs()
Copy link
Collaborator

Choose a reason for hiding this comment

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

I do not see the point of having self.log followed by self.save_logs. If both aim at doing the same then why not merging them ? If they do sth else, then write better comments.

Do not hesitate to question existing architecture because then you fall in trap like "I did it in log cause I didn't know what save_log was meant to do"

@@ -190,3 +196,13 @@ def symlink_external_files(self):

def save_logs(self):
pass

def log(self):
with open(os.path.join(self.main_dir, "generatemc.log"), 'a') as LOG_FILE:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why LOG_FILE is capitalized ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok so should I create a new pull request without them?

Copy link
Collaborator

Choose a reason for hiding this comment

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

It depends on your git knowledge. Your branch history is broken, see:
https://github.com/PanSzczerba/mcpartools/commits/feature/logging

Why commits b8c0b28 and f480569 are there under 26 of August ?

You can edit your commits history and do push --force or do new branch and new PR.

@PanSzczerba
Copy link
Contributor Author

Ok, so I think it should be fine right now. I have fixed commit history and did some minor changes You asked

log_file.write("\n")
log_file.write(time.strftime("%Y-%m-%d %H:%M:%S\n"))
log_file.write(getpass.getuser() + '@' + socket.gethostname() + "\n")
log_file.write(os.getcwd() + "\n")
Copy link
Collaborator

Choose a reason for hiding this comment

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

Add some description what is being logged, like:

Command executed: 
Date & time:
User@hostname:
Current directory:

@@ -225,4 +228,10 @@ def symlink_external_files(self):
os.symlink(abs_path, os.path.join(jobdir_path, os.path.split(abs_path)[-1]))

def save_logs(self):
pass
with open(os.path.join(self.main_dir, "generatemc.log"), 'a') as log_file:
Copy link
Collaborator

Choose a reason for hiding this comment

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

filename "generatemc.log" occurs now twice, see also here:

with open(os.path.join(log_location, "generatemc.log"), 'w+') as LOG_FILE:

        with open(os.path.join(log_location, "generatemc.log"), 'w+') as LOG_FILE:
            try:

What about unifying this ?

Can it be done in some generic way ?
Would it be possible to use python logging to write to such a file in some standarized way ?

See logging handlers https://docs.python.org/3.5/library/logging.html

@PanSzczerba
Copy link
Contributor Author

Ok so I tried using logging module. But I had to create another logger with it's own handler and I don't know if that's a good idea.

@antnieszka
Copy link
Contributor

antnieszka commented Aug 28, 2017

Maybe this could help:
https://github.com/DataMedSci/ProtonBeamsComposer/blob/master/pbc/optimize.py#L95
(I simply add a handle to global logger and it works just fine)

@antnieszka antnieszka added this to the Next release milestone Aug 28, 2017
@antnieszka antnieszka mentioned this pull request Aug 28, 2017
@@ -159,6 +166,8 @@ def generate_main_dir(self):
os.mkdir(dir_path)
self.main_dir = dir_path

generatemc_logger.addHandler(logging.FileHandler(os.path.join(dir_path, "generatemc.log"), mode='w+'))
Copy link
Collaborator

Choose a reason for hiding this comment

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

why "generatemc.log" occurs here and also in 'getLogger' method ?

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 wanted to add FileHandler directly after the main_dir path is assigned so it could log information about batch system in scheduler/common.py. And also I wanted to name this logger to be unique and so I could acces it in scheduler/common.py but I couldn't come up with a better idea how to name it.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Well, it could be named file_logger (the variable) and the logger name also file_logger

@@ -2,12 +2,19 @@
import logging
import shutil
import time
import sys
import getpass
import socket
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please sort imports.
https://google.github.io/styleguide/pyguide.html#Imports_formatting

Within each grouping, imports should be sorted lexicographically, ignoring case, according to each module's full package path.

logger.debug("Torque not found: %s", e)
file_logger = logging.getLogger('file_logger')
try:
file_logger.info(check_output(['srun --version'], shell=True))
Copy link
Collaborator

Choose a reason for hiding this comment

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

If I am correct check_output returns output of the command.
Please assign it first to the some variable, i.e. and log it with some description:

srun_output = check_output(...)
file_logger.info("srun info {:s}".format(...))
``

@@ -17,13 +17,15 @@ def __init__(self):
def get_scheduler(cls, scheduler_options, log_location):
file_logger = logging.getLogger('file_logger')
try:
file_logger.info(check_output(['srun --version'], shell=True))
srun_output = check_output(['srun --version'], shell=True)
file_logger.info("srun output: {}".format(srun_output[:-1]))
logger.debug("Discovered job scheduler SLURM")
Copy link
Contributor

@antnieszka antnieszka Aug 29, 2017

Choose a reason for hiding this comment

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

If it's debug logging level - I would add slurm/torque version to this log too. Same in line 29.

Copy link
Collaborator

Choose a reason for hiding this comment

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

It is already saved to the file (but not printed on screen), even on debug level:

# plgkongruencj at p1728 in /net/scratch/people/plgkongruencj/mcpartools on git:feature/logging ✖︎ [11:44:29]
→ cat aa/run_20170829_114429/generatemc.log 
srun output: slurm 17.02.7
Executed command: mcpartools/generatemc.py -j 10 -p 100 tests/res/shieldhit -w aa -vvv
Date and time: 2017-08-29 11:44:29
username@hostname: plgkongruencj@p1728
Current working directory: /net/scratch/people/plgkongruencj/mcpartools

Copy link
Collaborator

@grzanka grzanka left a comment

Choose a reason for hiding this comment

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

I approve this PR as it is
The issue is however not closed, there are more tasks to be done, which I will define on issue level.

@grzanka grzanka merged commit 4c8bc3c into DataMedSci:master Aug 29, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants