Apify Discord Mirror

Updated last year

Custom LoggerText implementation not handling objects

At a glance

A community member had to write a custom logger implementation to store logs in a file and rotate them. They extended the LoggerText class and implemented the methods, but encountered issues with skipping debug messages and handling objects passed to the logger. The community member looked at the source code and tried to stick to it as much as possible, and asked for help to identify the issues.

In the comments, another community member noted that in the log method, all the data being sent was treated as an exception. The original community member fixed the debug issue by changing the order of function variables. However, they encountered issues with the JSON adding backslashes everywhere, and the _parseException function not parsing the exception properly.

The community members discussed various fixes, including providing a link to a Pastebin with their solution, and discussing issues with the Crawlee implementation overriding their routes.ts settings. They also identified that the double encoding issue was due to Winston's JSON formatter.

There is no explicitly marked answer, but the community members provided various suggestions and fixes to address the issues with the custom logger implementation.

Useful resources
hey folks,
I had to write up a custom logger implementation because I needed to store the logs in a file and rotate them as well. I have extended LoggerText and have implemented the methods as well and while its working its skipping a lot of debug messages that we get with the inbuilt logger as well as not being able to handle objects being passed to it. I have looked at the source code for it and have stuck to it as much as I could.

I would really appreciate it if someone could point out whats wrong with it.

code: https://pastebin.com/tpGxyM0P

actual output: https://pastebin.com/ABtC8QSa
A
L
10 comments
on further debugging, it looks like in the log method, all of the data being sent is treated as exception
ok I fixed the debug stuff by flipping the order of function variables and making it like its there in the _log func
but the json is adding \ everywhere which I know is to skip double quotes but its not like that in the crawlee implementation, is there something I can do to fix it?
also I am calling _parseException just like in _log but its not parsing the exception at all and instead has undefined
and I am passing an exception along like this
Plain Text
try{
../code
}catch(error:any){
//any fixes for this any? it looks really ugly but ts wont let me pass it in without any
log.error('error',error)
}
Leaving this for others but this is how I fixed it https://pastebin.com/3awiV7ce
only issue I can find with crawlee's implementation is, I had to break out winston's logger in a separate function because crawlee's log would keep overriding my routes.ts settings if I imported a different file which was using a crawlee log too, Not sure if its supposed to be a bug or anything
as for double encoding, that was winston's json formatter shenanigans, we are already encoding our json into strings inside _log and winston was further encoding it as per my setup
Thanks, I will pass it to the team to make example
Add a reply
Sign up and join the conversation on Discord