i have console app interacts hardware. idea log exceptions stacktraces valuable information not lost due not having pipe file.
but how handle location of file? cant ask path each time.
that why thought put file next executable. cant sure app reside. , have not found distinct answer "how app path"
i found: new java.io.file("").getabsolutepath();
no explanation how works.
and: getclass().getprotectiondomain().getcodesource().getlocation().getpath();
op said may work. there way handle errorlogging / sure way app path?
the standard approach logging exceptions in types of applications use 1 of standard logging frameworks , defining configuration externally application. exceptions logged error
, handled appropriately.
one suggest e.g. slf4j (logging interface) logback (the matching implementation), other well.
when writing code, interface needs available @ compile time code programmed , compiled against it:
import org.slf4j.logger; import org.slf4j.loggerfactory; public class myclass { private static final logger logger = loggerfactory.getlogger(myclass.class); public void method() { try { ... } catch (ioexception ex) { logger.error("method failed expected: {}", ex); throw ex; }
a particular implementation needs provided @ runtime (e.g. added jar while packaging) or added classpath @ runtime.
next, configuration matching particular implementation needs provided @ runtime, logback may this:
<?xml version="1.0" encoding="utf-8"?> <configuration scan="true"> <appender name="file" class="ch.qos.logback.core.fileappender"> <file>myapp.log</file> <encoder> <pattern>%logger{35} - %msg%n</pattern> </encoder> </appender> <root level="error"> <appender-ref ref="file"/> </root> </configuration>
further details see under:
Comments
Post a Comment