Mailing List Archive

Breaking new relic format on a new line character in FileHandler appender
Hello Team,

I'm trying to add logs in the new relic platform from a python application.
For that, I've to add logs in a local file in a specific format which is

'{"log.level":"%(levelname)s", "log.entity.name":"my-service-name",
"message":"%(message)s"}'


This works fine in normal scenario and generates the below type of
line in log file and logs are added to new relic with expected
properties set like log level, entity name, message etc.

{"log.level":"INFO", "log.entity.name":"my-service-name", "message":"test"}


This issue occurs when a new line is present in the message property of the
log i.e. on any error we have a message like "Traceback (most recent call
last):" which represents an error traceback after this line with a new line
character. In this case, multiple logs are added instead of a single log,
my expected formatting breaks and logging properties are not set in the new
relic.

Sample breaking log:

{"log.level":"ERROR", "log.entity.name":"my-service-name",
"message":"[Errno 2] No such file or directory:
'E:\\comms-nlp-service\\src\\GoogleAuthentication\\xyz.json' -> Traceback
(most recent call last):
File "E:\comms-nlp-service\src\app\nlp\carrier_cancel_apiai.py", line 31,
in query
credentials =
service_account.Credentials.from_service_account_file(filename, scopes
=(scope,))
File "C:\Python34\lib\site-packages\google\oauth2\service_account.py",
line 209, in from_service_account_file
filename, require=['client_email', 'token_uri'])
File
"C:\Python34\lib\site-packages\google\auth\_service_account_info.py", line
71, in from_filename
with io.open(filename, 'r', encoding='utf-8') as json_file:
FileNotFoundError: [Errno 2] No such file or directory:
'E:\\comms-nlp-service\\src\\GoogleAuthentication\\ xyz .json'
"}

Any suggestions on this would be a great help.

--
Thanks,
Vijay Karavadra
--
https://mail.python.org/mailman/listinfo/python-list
Re: Breaking new relic format on a new line character in FileHandler appender [ In reply to ]
On Thu, Nov 11, 2021 at 5:00 PM Vijay Karavadra via Python-list
<python-list@python.org> wrote:
>
> Hello Team,
>
> I'm trying to add logs in the new relic platform from a python application.
> For that, I've to add logs in a local file in a specific format which is
>
> '{"log.level":"%(levelname)s", "log.entity.name":"my-service-name",
> "message":"%(message)s"}'
>

Looks like JSON to me. Have you considered using the json module to format it?

json.dumps({"log.level":levelname, "log.entity.name":"my-service-name",
"message":message})

ChrisA
--
https://mail.python.org/mailman/listinfo/python-list
Re: Breaking new relic format on a new line character in FileHandler appender [ In reply to ]
On 2021-11-10 05:16, Vijay Karavadra via Python-list wrote:
> Hello Team,
>
> I'm trying to add logs in the new relic platform from a python application.
> For that, I've to add logs in a local file in a specific format which is
>
> '{"log.level":"%(levelname)s", "log.entity.name":"my-service-name",
> "message":"%(message)s"}'
>
>
> This works fine in normal scenario and generates the below type of
> line in log file and logs are added to new relic with expected
> properties set like log level, entity name, message etc.
>
> {"log.level":"INFO", "log.entity.name":"my-service-name", "message":"test"}
>
>
> This issue occurs when a new line is present in the message property of the
> log i.e. on any error we have a message like "Traceback (most recent call
> last):" which represents an error traceback after this line with a new line
> character. In this case, multiple logs are added instead of a single log,
> my expected formatting breaks and logging properties are not set in the new
> relic.
>
> Sample breaking log:
>
> {"log.level":"ERROR", "log.entity.name":"my-service-name",
> "message":"[Errno 2] No such file or directory:
> 'E:\\comms-nlp-service\\src\\GoogleAuthentication\\xyz.json' -> Traceback
> (most recent call last):
> File "E:\comms-nlp-service\src\app\nlp\carrier_cancel_apiai.py", line 31,
> in query
> credentials =
> service_account.Credentials.from_service_account_file(filename, scopes
> =(scope,))
> File "C:\Python34\lib\site-packages\google\oauth2\service_account.py",
> line 209, in from_service_account_file
> filename, require=['client_email', 'token_uri'])
> File
> "C:\Python34\lib\site-packages\google\auth\_service_account_info.py", line
> 71, in from_filename
> with io.open(filename, 'r', encoding='utf-8') as json_file:
> FileNotFoundError: [Errno 2] No such file or directory:
> 'E:\\comms-nlp-service\\src\\GoogleAuthentication\\ xyz .json'
> "}
>
> Any suggestions on this would be a great help.
>
How about formatting using 'r' or 'a' instead of 's'?

'{"log.level": "%(levelname)s", "log.entity.name": "my-service-name",
"message": "%(message)r"}'
--
https://mail.python.org/mailman/listinfo/python-list