-
Notifications
You must be signed in to change notification settings - Fork 421
child=True
is not creating a logger?
#143
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I would suspect this is at fault: handlers = self._logger.parent.handlers if self.child else self._logger.handlers |
Hey Loren - Thanks for raising this issue. If you set service="foo" in your child in the first example it should work. We need to make the docs clearer here, so please allow me to explain what's going on (I'm on my phone so it'll be a longer response than usual) Service is what defines what the function is responsible for, or part of (e.g payment service). For Logger, the service is the logging key customers will use to search log operations for one or more functions, for example - "Search for all error, or messages like X, where service is payment". That said, customers can set the service using explicit arguments, but typically use as an environment variable since it's used across all utilities and to avoid repetition - POWERTOOLS_SERVICE_NAME With that hopefully explained, let's get to inheritance. Python Logging hierarchy happens at the dot notation (service, service.child, service.child_2). For child Loggers we introspect the name of your module where Logger(child=True) is called, and name your Logger as "service.filename". Once both Loggers are registered, and Python Logging recognises the dot notation, it fixes up the hierarchy and event propagation. Why the first sample didn't work Python Logger doesn't recognise as a parent + child loggers because their names are different, since service param value is different.
If you were to use the same service name in the Child logger that should work, as that will then be "foo.bar", where bar comes from bar.py Why the service key isn't foo.bar That's because of consistency. The service/application is still the same despite having multiple Loggers. Sorry for the long explanation but hope that clarifies it - Lemme know otherwise :) Thanks |
@heitorlessa Ok, perhaps I'm just not using this library correctly then. All the examples assume I suppose I don't particularly care about the service name for our use case. I do care about specifically where in the module the message was generated. It looks like this module is using the
|
That is correct - location is where funcName:lineNumber will be present, hence bar:lineNo We didn't include the name parameter for brevity, and for preventing mistakes such as name==name which wouldn't work. |
@heitorlessa personally I would prefer if "location" included the module it was benign logged from. This would be similar to other logging libraries.
|
Gotcha. Yeah, the function name is definitely not enough info. Is there some way we can set |
@michaelbrewer I think it might be easier to have an option to override log record attributes. When we started, we heard from beta customers the opposite, as the module wasn't always valuable. |
Maybe we can allow for a custom pattern for this. As in some cases you don't want the location field to be too long. |
That would be fine. Coming from python logging, we're used to setting the |
Yeah. I would want to suppress sample_rate, service and some other unused fields. Can overload "location" to include more info like the package/module and method. |
Maybe a separate RFC on a more tailored logger. |
That was mostly it @michaelbrewer as sometimes it'd be either too long or not easily parsed/searched. Let me ask you this before I go to sleep :) Apart from location, is there any other log record attribute you might to override? E.g timestamp etc Native ones under LogRecord attributes |
A RFC it is, then - if either of you could create I'd be happy to consider it. I want to avoid a situation where we add too many flags or break existing customers searching mechanisms (saved queries). Instead, we should provide an option for Power users to override Log records as they see fit without imposing this to less experienced customers, as Python Logging is a complex beast |
@heitorlessa - maybe some presets for location and timestamp. Support for pulling through correlation ids. And ability to suppress / change order of attributes. But I think the default should please as many people as possible. Small lambda with a single file should work as well as complex ones with sub modules. Maybe we have a simple flag to switch between the 2. |
Overriding the timestamp seems to work fine... My repro case here was simplified, but here is what we're really testing with, derived from our current logging configs...
Here's the boilerplate we currently have in all our lambdas, which your library is replacing, and also giving us the benefits of structured logging!
|
Ok, nice, so overriding
|
Yes, you can override "location", "timestamp", "level", and "datefmt" - I just haven't documented them. If you're okay with it being this way, then we just need:
I'll check the responses tomorrow |
@lorengordon @heitorlessa - looks like documentation updates and #140 might address this. # space.py
from aws_lambda_powertools import Logger
logger = Logger(location="%(module)s.%(funcName)s:%(lineno)d", timestamp=None, format_keys=["location", "message"])
def fly():
logger.info("takeoff")
fly() $ POWERTOOLS_SERVICE_NAME=falcon python space.py
{"location": "space.fly:7", "message": "takeoff", "level": "INFO", "service": "falcon", "sampling_rate": 0.0}
|
Agreed, given the ability to override |
We've just merged @michaelbrewer PR on ability to suppress and order a few keys - Working on a PR for the docs piece now |
@lorengordon @michaelbrewer could you please take a look and tell me whether this clarifies these and other questions? |
@heitorlessa looks good to me! thanks! |
@heitorlessa looks good |
This has now available in 1.5.0 release |
What were you trying to accomplish?
Re-use a logger in a sub-module, using the
child=True
feature, per the documentation.Expected Behavior
With the following setup, there should be two log entries, one with
"service": "foo"
and one with"service": "foo.bar"
.cat foo.py
:cat bar.py
:Current Behavior
Even setting the
service
explicitly in the submodule tofoo.bar
, the service remainsfoo
in the output, though at least now their are two log entries.cat bar.py
:Steps to Reproduce (for bugs)
See above.
Environment
The text was updated successfully, but these errors were encountered: