鉴于这个小片段:
import sys
import os
import logging
from PyQt5 import QtGui, QtWidgets, QtCore
log = logging.getLogger("Foo")
logging.basicConfig(
level=logging.INFO, format='%(levelname)s: %(filename)s - %(message)s')
log.setLevel(logging.DEBUG)
class ConsolePanelHandler(logging.Handler):
def __init__(self, parent):
logging.Handler.__init__(self)
self.parent = parent
def emit(self, record):
self.parent.write(self.format(record))
class Foo(QtWidgets.QWidget):
def __init__(self, parent=None):
QtWidgets.QWidget.__init__(self, parent)
self.textEdit = QtWidgets.QTextEdit(self)
self.textEdit.setLineWrapMode(QtWidgets.QTextEdit.NoWrap)
self.textEdit.setTextInteractionFlags(QtCore.Qt.TextSelectableByMouse)
vbox = QtWidgets.QVBoxLayout()
self.setLayout(vbox)
vbox.addWidget(self.textEdit)
def write(self, s):
self.textEdit.setFontWeight(QtGui.QFont.Normal)
self.textEdit.append(s)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
console_panel = Foo()
handler = ConsolePanelHandler(console_panel)
log.addHandler(handler)
log.info("Getting logger {0} - {1}".format(id(log), log.handlers))
[log.debug("This is normal text " + str(i)) for i in range(5)]
console_panel.show()
sys.exit(app.exec_())
问题:
log.handlers
len应该只有1. 答案 0 :(得分:2)
您的问题是由导入时调用 CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Entity> cq = cb.createQuery(Entity.class);
Root<Entity> r = cq.from(Entity.class);
List<Predicate> p = new ArrayList<>();
//conditionally create zero or more conditions
Predicate condition= cb.equal(r.get("fieldName"), user.getId());
p.add(condition);
if(Collections.isNotEmpty(p)){
Predicate[] pArray = p.toArray(new Predicate[]{});
Predicate predicate = cb.and(pArray);
cq.where(predicate);
}
cq.orderBy(cb.desc(r.get("fieldName")));
return em.createQuery(cq).getResultList();
引起的 - documented,如果它没有任何处理程序,则会向根记录器添加控制台记录器。
您需要删除此调用并在basicConfig()
子句中添加一行:
if __name__ == '__main__'
然后你应该得到你期望的结果。
您看到这两条消息的原因是因为信息在记录器和处理程序之间如何流动,这在文档here中有所描述。