hi, i am using IntelliJ. I have a `fun main()...` ...
# getting-started
e
hi, i am using IntelliJ. I have a
fun main()...
function that I execute. when I execute the main function, the program runs, but I know there are exceptions being thrown by my code but none get outputted to my console. How can I setup logging so that I can see the logs in the console?
f
Well it depends on where those exceptions are thrown and what logging framework you use
e
Hi,i'm using:
io.github.microutils:kotlin-logging:1.4.9
if i log statements in my project, it works. but the exceptions being thrown from a Jar dependency that i call do not get shown in the console
all i see is
Process finished with exit code 0
f
you'll have to check what logging framework this dependency uses and probably change some configs on your side to have their loggers show up in your log files/IDE (I've never used kotlin-logging so I don't really know how it works)
what's your dependency that's throwing the exception?
e
my own jar file
in my own jar file, i added:
compile 'org.slf4j:slf4j-api:1.7.10'
i read that for libraries, i only have to add that dependency
f
yeah but you still have to log whatever you want logged yourself
e
the exception not being logged is a NPE because some models are not marked as nullable
i'm an Android developer, and I'm used to every error is shown in the console
i guess it is a system exception, Kotlin telling me a property is not set as nullable
f
yeah NPEs are RuntimeExceptions
e
what can i do to show all these errors in the console?
f
either way, you won't see exceptions show up in the console if they aren't : 1: thrown in the main thread 2: explicitly logged in some logging framework
so you'd have to get a logger from SLF4J, wrap whatever is throwing your exception in a try/catch and do
logger.error(...)
with your caught exception
e
ok, so i run the code and on a breakpoint before the NPE i can see : "ForkJoinPool.commonPool-worker-1" in group "main" : RUNNING
so it is in the main thread
i just wrapped the call in a try catch but nothing is shown
f
if your thread name is "ForkJoinPool.commonPool-worker-1" you're definitely not in the main thread
either way I suggest you read a bit on how SLF4J works as you'll have other stuff to setup to make sure your logs are displayed correctly (if at all) : https://www.slf4j.org/docs.html
e
ok thanks.
removed the code from the background thread and now i'm seeing what i expected
indeed, it was not in the main thread
f
yeah that's what I figured 😉
and don't worry, no one understands the Java logging frameworks at first (still not sure I really understand either >_>)
d
Use
runBlocking
and
await
or
join
. CommonPool is the default thread pool dispatcher in coroutines... you need to wait for the result to come back from the thread...
e
@dave08 thanks. did that and it also works