Python的根记录器的名称是什么?

时间:2018-02-09 18:42:31

标签: python logging

我正在学习有关使用Python登录的基本教程https://docs.python.org/3/howto/logging.html#a-simple-example,并按照iPython控制台中的步骤进行操作。如此处所述,根记录器的默认级别为WARNING

In [1]: import logging

In [2]: logging.warning('Watch out!')
WARNING:root:Watch out!

In [3]: logging.info('I told you so')

我想将根记录器的级别显式设置为INFO。我尝试使用name='root'来做,但这没有明显的效果:

In [4]: logging.getLogger('root').setLevel(logging.INFO)

In [5]: logging.info('I told you so')

如果我在没有参数的情况下调用logging.getLogger(),我可以设置根记录器的级别:

In [6]: logging.getLogger().setLevel(logging.INFO)

In [7]: logging.info('I told you so')
INFO:root:I told you so
但是,我很好奇,为什么这第一次不起作用?这似乎应该有效,因为它的name属性是'root':

In [12]: root_logger = logging.getLogger()

In [14]: root_logger.name
Out[14]: 'root'

简而言之,如果我不想依赖默认值,那么为了获得根记录器,我会将{I-name}传递给logging.getLogger()

1 个答案:

答案 0 :(得分:3)

根记录器的名称为root,但您无法按名称访问根记录器。

检查logging.py揭示了这一点:

def __init__(self, root):
    """
    Initialize the manager with the root node of the logger hierarchy.
    """
    self.root = root
    self.disable = 0
    self.emittedNoHandlerWarning = 0
    self.loggerDict = {}

rootloggerDict分开存储,def getLogger(name=None): """ Return a logger with the specified name, creating it if necessary. If no name is specified, return the root logger. """ if name: return Logger.manager.getLogger(name) else: return root 是所有指定记录器所在的位置。

manager.getLogger

您可以关注root内部功能,发现找不到def getLogger(self, name): """ Get a logger with the specified name (channel name), creating it if it doesn't yet exist. If a PlaceHolder existed for the specified name [i.e. the logger didn't exist but a child of it did], replace it with the created logger and fix up the parent/child references which pointed to the placeholder to now point to the logger. """ rv = None _acquireLock() try: if self.loggerDict.has_key(name): rv = self.loggerDict[name] if isinstance(rv, PlaceHolder): ph = rv rv = _loggerClass(name) rv.manager = self self.loggerDict[name] = rv self._fixupChildren(ph, rv) self._fixupParents(rv) else: rv = _loggerClass(name) rv.manager = self self.loggerDict[name] = rv self._fixupParents(rv) finally: _releaseLock() return rv

@{
    ViewBag.Title = "Email";
    var X = Html.X();
}

@section scripts
{
<script>
    var InstructionScript = function (grid) {
        grid.columns[2].setVisible(false); //--> How can I check if the 
                                          value of columns 2 is blank here ?
    }
</script>
}

@(X.Window()
        .ID("WindowDetail")
        .Title("Email Detail ")
        .Hidden(true)
        .Modal(true)
        .Listeners(events => events.Show.Handler = "InstructionScript(#
         {GridPanelDetail})")
        .Items(
            X.FormPanel().ID("FormDetail").Region(Region.North).Border(true).MarginSpec("5 5 5 5").Layout(LayoutType.Column).BodyPadding(5)
            .Items(
                X.FieldSet().ColumnWidth(.5).Layout(LayoutType.Anchor).Border(false)
                    .Defaults(d => { d.Add(new Parameter("LabelWidth", "115")); })
                    .Items(
                        X.TextField().FieldLabel("Email").DataIndex("Email").Editable(false).AnchorHorizontal("100%")
                    )  //Items
                , X.FieldSet().ColumnWidth(.5).Layout(LayoutType.Anchor).Border(false).Defaults(d =>
                    { d.Add(new Parameter("LabelWidth", "115"));})
                    .Items(
                            X.TextField().FieldLabel("Email Sent").DataIndex("EventDate").Editable(false).AnchorHorizontal("100%")
                    )  //Items
                )  //Items
                , X.GridPanel().ID("GridPanelDetail").Region(Region.Center).Resizable(true).Border(true).MarginSpec("0 5 5 5")
                        .Store(X.StoreFor<myproject.model.emailviewmodel>().ID("EmailStore")
                            .Parameters(p => p.Add(new StoreParameter("id", "App.Id.getValue()", ParameterMode.Raw)))
                            .Proxy(X.AjaxProxy()
                                .Url(Url.Action("ReadData"))
                                .Reader(X.JsonReader().RootProperty("data"))
                            )  //Proxy
                        .DataSource(Model)
                        .GroupField("Email")
                        )  //Store
.ColumnModel(
X.RowNumbererColumn().Width(25).Text("#").Sortable(false).Groupable(false).Hideable(false)
, X.Column().Text("Name").DataIndex("Name").Width(150).Groupable(false).Hideable(false)
, X.Column().ID("Instruction").Text("Instruction").DataIndex("Instruction").MinWidth(250).Flex(1).Groupable(false).Hideable(true)
)  //ColumnModel
        )  //Items
)  //X.Window