https://kotlinlang.org logo
Title
e

edhu

02/15/2018, 5:32 PM
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

fred.deschenes

02/15/2018, 5:53 PM
Well it depends on where those exceptions are thrown and what logging framework you use
e

edhu

02/15/2018, 6:02 PM
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

fred.deschenes

02/15/2018, 6:22 PM
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

edhu

02/15/2018, 6:23 PM
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

fred.deschenes

02/15/2018, 6:25 PM
yeah but you still have to log whatever you want logged yourself
e

edhu

02/15/2018, 6:26 PM
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

fred.deschenes

02/15/2018, 6:30 PM
yeah NPEs are RuntimeExceptions
e

edhu

02/15/2018, 6:30 PM
what can i do to show all these errors in the console?
f

fred.deschenes

02/15/2018, 6:31 PM
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

edhu

02/15/2018, 6:35 PM
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

fred.deschenes

02/15/2018, 6:39 PM
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

edhu

02/15/2018, 6:42 PM
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

fred.deschenes

02/15/2018, 6:46 PM
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

dave08

02/16/2018, 3:47 AM
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

edhu

02/16/2018, 10:20 AM
@dave08 thanks. did that and it also works