我只有几个脚本,可以在其中多播放一些代码,然后开始进行折光处理。 下面是代码:
1)在脚本名为log的位置创建目录
2)将信息放入log / file_name.log
下面是代码:
# Get path where script is
pathABSofScript = ""
if platform.system() == "Linux":
pathABSofScript = str(os.path.realpath(__file__).rsplit('/', 1)[0]) + "/"
else:
print("Unknown operating system")
exit(1)
if debug == "on":
print("Absolute path for script: {0}".format(pathABSofScript))
# Create directory
directories = ['log']
for directory in directories:
try:
os.makedirs("{0}{1}".format(pathABSofScript, directory))
except FileExistsError as ex:
pass
except Exception as ex:
logger.error("An exception of typ {0} occurred in function {2}. Arguments:\n{1!r} exiting.".format(type(ex).__name__, ex.args, sys._getframe(2).f_code.co_name))
exit(1)
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh = handlers.RotatingFileHandler('{0}/log/scriptName.log'.format(pathABSofScript), maxBytes=1000000,
backupCount=10)
fh.setLevel(logging.DEBUG)
fh.setFormatter(formatter)
logger.addHandler(fh)
logger.info("Started")
As every my script has the same code I would like to create directory lib and call this code logger.py.
我已经创建了如下目录结构:
scripts/
lib/
logger.py
someScript/
someScript.py
以前,在someScript目录中,脚本创建了日志目录,然后将信息记录在someScript / log / someScript.log文件中。
当我将代码移到logger.py文件中并在someScrip.py中导入
sys.path.insert(0, '/me01/apps/onetick/apps/config-test/lib')
import logger
from logger import logger
创建的日志目录位于lib /目录内,而不是someScript /-如何更改该目录?我试图在someScript.py中传递变量:
logger.pathABSofScript = "someScript/"
但是没有用。
如何将变量导入logger.py并将其导入someScript.py中?
答案 0 :(得分:0)
类似的答案在这里:
https://rextester.com/TLIPO27824
基本上,您必须使用以下命令创建另一个文件夹,名为“ my_config”
其中的__init__.py
脚本。
因此,您的项目的结构将是:
scripts/
my_config/
__init__.py
lib/
logger.py
someScript/
someScript.py
然后,您必须将该代码放入my_config/__init__.py
脚本中:
PATH_TO_MY_SOURCE = '/default/path'
接下来,编辑您的lib / logger:
import my_config
PATH_TO_MY_SOURCE = my_config.PATH_TO_MY_SOURCE
...
最后,在someScript.py中:
import my_config
import os
my_config.PATH_TO_MY_SOURCE = os.path.dirname(os.path.realpath(__file__))
from logger import logger
...
答案 1 :(得分:0)
感谢@ alex-b分享他的想法。 我已经找到了另一种解决方案:
我的logger.py看起来像这样:
WKApplicationRefreshBackgroundTask.setTaskCompletedWithSnapshot()
我正在someScript.py中创建
import logging
import logging.handlers as handlers
import sys
import os
# IT IS NOT WORKING WITHOUT THIS LINE BELOW, DUNNO WHY
logger = None
def create_logger(path, file_name):
global logger
try:
os.makedirs("{0}log".format(path))
except FileExistsError as ex:
pass
except Exception as ex:
logger.error("An exception of typ {0} occurred in function {2}. Arguments:\n{1!r} exiting.".format(
type(ex).__name__, ex.args, sys._getframe(2).f_code.co_name))
exit(1)
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh = handlers.RotatingFileHandler('{0}log/{1}'.format(path, file_name), maxBytes=1000000,
backupCount=10)
fh.setLevel(logging.DEBUG)
fh.setFormatter(formatter)
logger.addHandler(fh)
logger.info("Started")
def info(msg):
try:
if isinstance(msg, str):
logger.info(msg)
except Exception as ex:
logger.error("An exception of typ {0} occurred in function {2}. Arguments:\n{1!r} exiting."
.format(type(ex).__name__, ex.args, sys._getframe(2).f_code.co_name))
exit(1)
def debug(msg):
try:
if isinstance(msg, str):
logger.debug(msg)
except Exception as ex:
logger.error("An exception of typ {0} occurred in function {2}. Arguments:\n{1!r} exiting."
.format(type(ex).__name__, ex.args, sys._getframe(2).f_code.co_name))
exit(1)
def warning(msg):
try:
if isinstance(msg, str):
logger.warning(msg)
except Exception as ex:
logger.error("An exception of typ {0} occurred in function {2}. Arguments:\n{1!r} exiting."
.format(type(ex).__name__, ex.args, sys._getframe(2).f_code.co_name))
exit(1)
def error(msg):
try:
if isinstance(msg, str):
logger.error(msg)
except Exception as ex:
logger.error("An exception of typ {0} occurred in function {2}. Arguments:\n{1!r} exiting."
.format(type(ex).__name__, ex.args, sys._getframe(2).f_code.co_name))
exit(1)