-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New feature for error and logs handling #235
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
magar51
wants to merge
14
commits into
ndleah:main
Choose a base branch
from
magar51:feat/error_logs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
538616f
first commit
magar51 35e7599
Update calculator.py
magar51 308d87f
Merge pull request #1 from magar51/calc_update
magar51 f4919c4
New unit added for volume calculation
magar51 d97416a
Merge pull request #2 from magar51/feat/conv_update
magar51 a1d6a3b
New functionality for temperature conversion added
magar51 fe700e7
New functionality for temperature conversion added
magar51 c9bfcb7
New functionality for temperature conversion added
magar51 a26c943
Merge pull request #3 from magar51/feat/conv_update
magar51 e703395
New feature for error and logs handling
magar51 42845e0
Merge pull request #4 from magar51/feat/error_logs
magar51 8d5aa16
Merge branch 'ndleah:main' into main
magar51 63ab01b
Merge branch 'main' of https://github.aexp.com/magar51/python-mini-pr…
magar51 f28cd00
Update README.md
magar51 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
 | ||
 | ||
[](https://github.com/ndleah) | ||
[](https://github.com/ndleah?tab=repositories) | ||
|
||
# Execute Shell Command | ||
|
||
## 🛠️ Description | ||
Python code is written to perform below 3 functionalities: | ||
1. Execute shell command and get the output and error. | ||
2. Logging of all steps | ||
3. Archiving of logs | ||
|
||
## ⚙️ Languages or Frameworks Used | ||
Language used is Python. There are no dependencies. | ||
|
||
## 🌟 User Inputs | ||
There are 3 inputs required from user before executing the code: | ||
1. Email Id | ||
2. Log path | ||
3. Archive Log path | ||
|
||
Make sure to provide correct path for log, archive log and email_id in python file before executing it. | ||
|
||
## 🌟 How to run | ||
Simply import required funtions in your Python script and execute it. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
"""This script aims at creating log file and sending it on email""" | ||
import os | ||
import sys | ||
import glob | ||
import logging | ||
import datetime | ||
from shutil import move | ||
import subprocess | ||
|
||
|
||
"""Generic error handling module""" | ||
def error(status, error_msg, module_name, return_code): | ||
|
||
if status.lower() == 'error': | ||
logging.error('!!! Error !!!') | ||
logging.error('Error executing module : %s', module_name) | ||
logging.error(error_msg) | ||
logging.error('Module return code : %s', return_code) | ||
logging.error('Exiting script with exit(1) !!!') | ||
email_log_file(log_file, status='failure') | ||
sys.exit(1) | ||
|
||
if status.lower() == 'warning': | ||
logging.warning('!!! Warning !!!') | ||
logging.warning('Error executing module : %s', module_name) | ||
logging.warning(error_msg) | ||
logging.warning('Module return code : %s', return_code) | ||
logging.warning('Script execution will continue !!!') | ||
|
||
|
||
"""Generic Logging function """ | ||
def initiate_logging(): | ||
|
||
dt_now = datetime.datetime.today() | ||
log_file = '<file_path>' + 'testing' + dt_now.strftime('_%Y%m%d_%H%M%S') + '.log' | ||
log_file_pattern = '<file_path>' + 'testing' + '*' + '.log' | ||
log_file_list = glob.glob(log_file_pattern) | ||
|
||
for file in log_file_list: | ||
if os.path.exists(file): | ||
dest_file = '<archive_log_path>' + file.split('/')[-1] | ||
if os.path.exists(dest_file): | ||
os.remove(dest_file) | ||
move(file, '<archive_log_path>') | ||
|
||
logging.basicConfig(filename=log_file, level=logging.DEBUG, | ||
format='%(asctime)s : %(levelname)s : %(message)s', datefmt='%Y-%m-%d %H:%M:%S') | ||
|
||
return log_file | ||
|
||
|
||
"""Executes the provided unix shell command """ | ||
def execute_shell_command(command): | ||
|
||
try: | ||
proc = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) | ||
out, err = proc.communicate() | ||
return_code = proc.returncode | ||
if err: | ||
print(str(err)) | ||
return out, return_code | ||
except Exception as err: | ||
print("Exception Occurred while executing module : %s", str(err)) | ||
return 105 | ||
|
||
|
||
"""Emails the status of job execution along with log file as attachment""" | ||
def email_log_file(log_file, status): | ||
|
||
if status == 'success': | ||
body = 'Execution successful...PFA log file' | ||
subject = 'Execution successful - testing' | ||
|
||
email_cmd = """echo "{b}" | mailx -s "{s}" -a "{l}" "{to}" """.format(b=body, s=subject, l=log_file, to='<email_id>') | ||
logging.info('Email Command :- %s', email_cmd) | ||
os.system(email_cmd) | ||
else: | ||
body = 'Execution Failed...PFA log file' | ||
subject = '!!! Failure - testing !!!' | ||
|
||
email_cmd = """echo "{b}" | mailx -s "{s}" -a "{l}" "{to}" """.format(b=body, s=subject, l=log_file, to='<email_id>') | ||
logging.info('Email Command :- %s', email_cmd) | ||
os.system(email_cmd) | ||
|
||
|
||
# Start of Execution | ||
if __name__ == '__main__': | ||
|
||
log_file = initiate_logging() | ||
|
||
logging.info('!!! Execution Begins !!!') | ||
|
||
command ='echo Madhuri' | ||
result, status = execute_shell_command(command) | ||
|
||
if status != 0: | ||
logging.info("Exception in running shell command") | ||
STATUS = 'error' | ||
ERROR_MSG = '!!! Error !!! Execution failed !!!' | ||
MODULE_NAME = 'Execution Script' | ||
RETURN_CODE = '' | ||
error(STATUS, ERROR_MSG, MODULE_NAME, RETURN_CODE) | ||
else: | ||
command_result = result.decode('utf8').rstrip("\r\n") | ||
logging.info(command_result) | ||
|
||
email_log_file(log_file, status='success') | ||
# End of Execution |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please do not alter
README.md
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a new readme.md file in respective folder, not the main readme file.
Error and Logs Handling/README.md