Skip to content

Commit

Permalink
fix bug:correct handle when meets PermissionError (#460)
Browse files Browse the repository at this point in the history
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

- **Bug Fixes**
  - Improved log file path creation logic to enhance reliability.
  - Added detailed log messages for file initialization.
- Enhanced error handling for permission issues when creating log files.
  - Adjusted log handlers for better log management.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
  • Loading branch information
felix5572 authored May 27, 2024
2 parents 661882a + 3725aee commit ec6081b
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions dpdispatcher/dlog.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,25 @@
dlog = logging.getLogger("dpdispatcher")
dlog.propagate = False
dlog.setLevel(logging.INFO)
cwd_logfile_path = os.path.join(os.getcwd(), "dpdispatcher.log")
dlogf = logging.FileHandler(cwd_logfile_path, delay=True)
try:
dlogf = logging.FileHandler(
os.getcwd() + os.sep + "dpdispatcher" + ".log", delay=True
)
dlog.addHandler(dlogf)
dlog.info(f"LOG INIT:dpdispatcher log direct to {cwd_logfile_path}")
except PermissionError:
dlog.removeHandler(dlogf)
warnings.warn(
"dpdispatcher.log meet permission error. redirect the log to ~/dpdispatcher.log"
f"dump logfile dpdispatcher.log to {cwd_logfile_path} meet permission error. redirect the log to ~/dpdispatcher.log"
)
dlogf = logging.FileHandler(
os.path.join(os.path.expanduser("~"), "dpdispatcher.log"), delay=True
)
dlog.addHandler(dlogf)
dlog.info("LOG INIT:dpdispatcher log init at ~/dpdispatcher.log")

dlogf_formatter = logging.Formatter("%(asctime)s - %(levelname)s : %(message)s")
dlogf.setFormatter(dlogf_formatter)
dlog.addHandler(dlogf)
# dlog.addHandler(dlogf)

dlog_stdout = logging.StreamHandler(sys.stdout)
dlog_stdout.setFormatter(dlogf_formatter)
Expand Down

0 comments on commit ec6081b

Please sign in to comment.