Coverage for sources/ictruck/recipes/logging.py: 100%
23 statements
« prev ^ index » next coverage.py v7.9.2, created at 2025-07-05 05:21 +0000
« prev ^ index » next coverage.py v7.9.2, created at 2025-07-05 05:21 +0000
1# vim: set filetype=python fileencoding=utf-8:
2# -*- coding: utf-8 -*-
4#============================================================================#
5# #
6# Licensed under the Apache License, Version 2.0 (the "License"); #
7# you may not use this file except in compliance with the License. #
8# You may obtain a copy of the License at #
9# #
10# http://www.apache.org/licenses/LICENSE-2.0 #
11# #
12# Unless required by applicable law or agreed to in writing, software #
13# distributed under the License is distributed on an "AS IS" BASIS, #
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
15# See the License for the specific language governing permissions and #
16# limitations under the License. #
17# #
18#============================================================================#
21''' Recipes for integration with standard logging. '''
25import logging as _logging
27from . import __
30_validate_arguments = (
31 __.validate_arguments(
32 globalvars = globals( ),
33 errorclass = __.exceptions.ArgumentClassInvalidity ) )
36InstallAdditionalAliasesArgument: __.typx.TypeAlias = __.typx.Annotated[
37 __.Absential[ __.cabc.Mapping[ str, str ] ],
38 __.typx.Doc(
39 ''' Mapping of builtins attribute names to flavors. ''' ),
40]
43@_validate_arguments
44def install(
45 alias: __.InstallAliasArgument = __.builtins_alias_default,
46 additional_aliases: InstallAdditionalAliasesArgument = __.absent,
47) -> __.Truck:
48 ''' Produces truck and installs it into builtins with alias.
50 Replaces an existing truck, preserving global module configurations.
52 Library developers should call :py:func:`__.register_module` instead.
53 '''
54 truck = produce_truck( ).install( alias = alias )
55 if __.is_absent( additional_aliases ): additional_aliases = { }
56 for falias, flavor in additional_aliases.items( ):
57 __.install_builtin_safely(
58 falias, truck( flavor ), __.exceptions.AttributeNondisplacement )
59 return truck
62@_validate_arguments
63def produce_printer( mname: str, flavor: __.Flavor ) -> __.Printer:
64 ''' Produces printer which maps flavors to logging levels. '''
65 logger = _logging.getLogger( mname )
66 level = (
67 getattr( _logging, flavor.upper( ) ) if isinstance( flavor, str )
68 else _logging.DEBUG )
69 return lambda x: logger.log( level, x )
72@_validate_arguments
73def produce_truck( ) -> __.Truck:
74 ''' Produces icecream truck which integrates with standard logging. '''
75 active_flavors = { None: frozenset( {
76 'debug', 'info', 'warning', 'error', 'critical' } ) }
77 flavors: __.immut.Dictionary[ __.Flavor, __.FlavorConfiguration ] = (
78 __.immut.Dictionary(
79 { name: __.FlavorConfiguration( )
80 for name in active_flavors[ None ] } ) )
81 generalcfg = __.VehicleConfiguration( flavors = flavors )
82 nomargs = dict(
83 active_flavors = active_flavors,
84 generalcfg = generalcfg,
85 printer_factory = produce_printer )
86 return __.Truck( **nomargs ) # pyright: ignore