我不知道为什么这段代码打印到屏幕上,而不是文件?创建了文件"example1.log",但没有写入任何内容.
#!/usr/bin/env python3
import logging
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s %(message)s',
handlers=[logging.FileHandler("example1.log"),
logging.StreamHandler()])
logging.debug('This message should go to the log file and to the console')
logging.info('So should this')
logging.warning('And this, too')
Run Code Online (Sandbox Code Playgroud)
我通过创建一个日志记录对象(示例代码)来"绕过"这个问题,但它一直困扰我为什么basicConfig()方法失败?
PS.如果我将basicConfig调用更改为:
logging.basicConfig(level=logging.DEBUG,
filename="example2.log",
format='%(asctime)s %(message)s',
handlers=[logging.StreamHandler()])
Run Code Online (Sandbox Code Playgroud)
然后所有日志都在文件中,控制台中不显示任何内容
可能重复:
为什么在这种情况下不需要全局关键字?
我想知道为什么我可以在没有global关键字的情 为什么它对其他类型是强制性的?这背后有什么逻辑吗?
例如代码:
#!/usr/bin/env python3
stringvar = "mod"
dictvar = {'key1': 1,
'key2': 2}
def foo():
dictvar['key1'] += 1
def bar():
stringvar = "bar"
print(stringvar)
print(dictvar)
foo()
print(dictvar)
print(stringvar)
bar()
print(stringvar)
Run Code Online (Sandbox Code Playgroud)
给出以下结果:
me@pc:~/$ ./globalDict.py
{'key2': 2, 'key1': 1}
{'key2': 2, 'key1': 2} # Dictionary value has been changed
mod
bar
mod
Run Code Online (Sandbox Code Playgroud)
我期待的地方:
me@pc:~/$ ./globalDict.py
{'key2': 2, 'key1': 1}
{'key2': 2, 'key1': 1} # I didn't use global, so dictionary remains the same
mod
bar
mod
Run Code Online (Sandbox Code Playgroud) Pylint W0603说:
使用全局声明.在使用"global"语句更新全局变量时使用.PyLint只是试图阻止这种用法.这并不意味着你不能使用它!
我想知道为什么会这样?是否有更多Pythonic方法在函数内修改不可变的模块范围变量?是否建议将它们打包在像字典这样的可变变量中?或者可能将整个模块变成课堂?
在我看来,当变量被建议为"私有"(以_或__为前缀)时,此警告应该消失.
我已经安装了新的pytest插件(pytest-catchlog==1.2.2),并且我喜欢它,它打破了我对日志模块的单元测试(例如ValueError: I/O operation on closed file).
我想为test_logging.py文件(甚至是类或方法)禁用该插件,但我找不到任何关于它的信息.
到目前为止,我发现的唯一选项是执行两次pytest:第一次用于test_logging.py,只有catchlog disabled(py.test -p no:catchlog test_logging.py),第二次用于所有其他测试文件.
如果我错过了pytest装饰器,或者在运行时禁用插件的任何其他方式,请告诉我.
我只是注意到Python 3.2已从Python Docs的下拉选择器中删除。我有一个使用Python 3.3的框架,所以我想知道它何时会具有相同的命运,并在发生这种情况之前移至新版本。
是否有关于Python 3.x获得支持,反向移植等时间的官方指南?我在Python的主页上或通过搜索引擎找不到有关Python发布生命周期的任何信息。
Pylint不断报告R: 73,0:MyLogging: Too many public methods (22/20)以下代码的error():
class MyLogging(logging.Logger):
def foo(self):
pass
def bar(self):
pass
Run Code Online (Sandbox Code Playgroud)
起初我认为这是Pylint中的一个错误,因为MyLogging类只有22行代码,但是我意识到,它包括了基类中的所有公共方法logging.Logger,这增加了20个统计数据.
您知道是否可以从Pylint统计中排除基类公共方法?
PS.我知道我可以更改max-public-methods为更高的数字,或者添加一次异常# pylint: disable=R0904
在 Linux 上运行的 .net-core 应用程序是否可以向子进程发送 SIGTERM 信号?
我们正在考虑将我们的 .net 应用程序移植到 .net-core 并在 Linux 上运行它,以避免当前的信号实现(即CloseMainWindow()从父进程发送,并使用 pywin32 包,以及SetConsoleCtrlHandler()在子进程中)
python ×6
global ×2
pylint ×2
python-3.x ×2
.net-core ×1
console ×1
dictionary ×1
file ×1
logging ×1
plugins ×1
pytest ×1
python-3.3 ×1
scope ×1
screen ×1
signals ×1