In my Scala project I'am using the phantom-sbt plugin in order to start embedded Cassandra. The problem is, this plugin is pretty verbose - all cassandra logs will be written to stdout.
I've seen on phantom github page, they are using log4j to configure all loggers. But it seems not to work (at least for me). I've set all loggers in the log4j.xml on 'ERROR', but it has no effect.
How should I change log level for all cassandra loggers?
You need a logback-test.xml inside /src/test/resources wherever you are running the embedded Cassandra. Then you can easily turn off individual loggers or set them to the appropriate levels.
Take this as an example configuration:
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- encoders are assigned the type
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="debug">
<appender-ref ref="STDOUT" />
</root>
<logger name="com.datastax.driver.core" level="ERROR"/>
<logger name="io.netty" level="ERROR"/>
<logger name="org.cassandraunit" level="ERROR"/>
Related
I am using the library scalikejdbc for my Application. And when i read the logs, that scalikejdbc generates itself, it gives me something like that:
06:02:16.891 [main] DEBUG scalikejdbc.ConnectionPool$ - Registered connection pool : ConnectionPool(url:jdbc:sqlserver://foo.bar:8080;databaseName=foobar;user=user;password=**PASSWORD**;...
So scalike itself throws my database user's password into the logs, which is inappropriate.
I was thinking about filtering those logs at all with something like https://logback.qos.ch/manual/filters.html , but I do need the odd information from those logs from scalike, so I cannot filter them fully.
What do I have now:
06:02:16.891 [main] DEBUG scalikejdbc.ConnectionPool$ - Registered connection pool : ConnectionPool(url:jdbc:sqlserver://foo.bar:8080;databaseName=foobar;user=user;password=somepassword;
What am I try to get:
06:02:16.891 [main] DEBUG scalikejdbc.ConnectionPool$ - Registered connection pool : ConnectionPool(url:jdbc:sqlserver://foo.bar:8080;databaseName=foobar;user=user;password=CENSORED;
Consider using replace conversion word in logback.xml configuration, for example,
%replace(%msg){"password=.*", "password=CENSORED"}
which given
logger.debug("password=123456")
should output something like
[debug] application - password=CENSORED
Docs state
replace(p){r, t} Replaces occurrences of 'r', a regex, with its
replacement 't' in the string produces by the sub-pattern 'p'. For
example, "%replace(%msg){'\s', ''}" will remove all spaces contained
in the event message.
Here is an example of my logback.xml:
<configuration>
<conversionRule conversionWord="coloredLevel" converterClass="play.api.libs.logback.ColoredLevel" />
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%coloredLevel %logger{15} - %replace(%msg){"password=.*", "password=CENSORED"}%n %xException{10}</pattern>
</encoder>
</appender>
<logger name="play" level="INFO" />
<logger name="application" level="DEBUG" />
<logger name="com.gargoylesoftware.htmlunit.javascript" level="OFF" />
<root level="WARN">
<appender-ref ref="STDOUT" />
</root>
</configuration>
I'm on Scala 2.12.x Play 2.7.x and as part of my Application Controllers I'm using the logger like this (or equivalently by doing with play.api.Logging):
val logger = play.api.Logger(this.getClass)
and the configuration conf/logback.xml is shown below. The issue is that Application-Controllers' logger.debug(...) statements are not being output in the logs. I assumed that the application was set to DEBUG see one of the lasts entries <logger name="application" level="DEBUG" /> but there is obviously something wrong as the DEBUG statements are not being output?
<!-- https://www.playframework.com/documentation/latest/SettingsLogger -->
<configuration>
<conversionRule conversionWord="coloredLevel" converterClass="play.api.libs.logback.ColoredLevel" />
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>${application.home:-.}/logs/application.log</file>
<encoder>
<pattern>%date [%level] from %logger in %thread - %message%n%xException</pattern>
</encoder>
</appender>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%coloredLevel %logger{15} - %message%n%xException{10}</pattern>
</encoder>
</appender>
<appender name="ASYNCFILE" class="ch.qos.logback.classic.AsyncAppender">
<appender-ref ref="FILE" />
</appender>
<appender name="ASYNCSTDOUT" class="ch.qos.logback.classic.AsyncAppender">
<appender-ref ref="STDOUT" />
</appender>
<logger name="play" level="INFO" />
<logger name="application" level="DEBUG" />
<logger name="slick" level="INFO" />
<logger name="slick.jdbc" level="DEBUG" />
<root level="WARN">
<appender-ref ref="ASYNCFILE" />
<appender-ref ref="ASYNCSTDOUT" />
</root>
</configuration>
PS: I know I can enable DEBUG for everything but then it is too much information I don't need, I'd like to see what's happening with my Controllers' logging.
The logger instantiated with play.api.Logger(this.getClass) inside a class like so
package controller
class HomeController extends ... {
val logger = play.api.Logger(this.getClass)
logger.debug("msg")
}
is not the application logger, but a distinct logger named
controller.HomeController
Thus to specify its log level add the following config to logback.xml
<logger name="controllers" level="DEBUG" />
This makes DEBUG the log level of all classes under controllers package.
Note application logger seems to be deprecated according to docs:
There is also a play.api.Logger singleton object that allows you to
access a logger named application, but its use is deprecated in Play
2.7.0 and above. You should declare your own logger instances...
If the messages are still not logging try changing from async appender to synchronous ones like so:
<root level="WARN">
<appender-ref ref="FILE" />
<appender-ref ref="STDOUT" />
</root>
I am trying to setup my logger instances to send mails whenever an error log is generated by the application. I thought of using gmail for this purpose. My logger.xml is as below:
<configuration>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>/path-to-application/application.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- rollover daily -->
<fileNamePattern>/path-to-application/application-%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<!-- or whenever the file size reaches 250MB -->
<maxFileSize>250MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%date - %thread [%level] - %message%n%xException%n</pattern>
</encoder>
</appender>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%date - %thread [%level] - %message%n%xException%n</pattern>
</encoder>
</appender>
<appender name="EMAIL" class="ch.qos.logback.classic.net.SMTPAppender">
<smtpHost>smtp.gmail.com</smtpHost>
<smtpPort>465</smtpPort>
<ssl>true</ssl>
<username>abc#gmail.com</username>
<password>abc's password</password>
<asynchronousSending>false</asynchronousSending>
<to>xyz#gmail.com#gmail.com</to>
<!--<to>ANOTHER_EMAIL_DESTINATION</to> <!– additional destinations are possible –>-->
<from>abc#gmail.com</from>
<subject>TESTING: %logger{20} - %m</subject>
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>%date %-5level %logger{35} - %message%n</pattern>
</layout>
<cyclicBufferTracker class="ch.qos.logback.core.spi.CyclicBufferTracker">
<!-- send just one log entry per email -->
<bufferSize>1</bufferSize>
</cyclicBufferTracker>
</appender>
<logger name="play" level="WARN"/>
<logger name="application" level="INFO"/>
<root level="ERROR">
<!-- <appender-ref ref="STDOUT" /> -->
<appender-ref ref="EMAIL"/>
</root>
</configuration>
I added javax.mail and activation as dependencies in my SBT file. The Rolling file appender and console appender are working fine but SMTP appender seems not to work with these settings. Error logs are being logged in my rolling file but not being send as mail. There are no exceptions being logged so that I could investigate further.
Is there something wrong with these specifications or problem might be from gmail smtp server end?
I had the same problem, and finally found out via debugging.
Logback cannot authenticate you to Gmail if you have setup 2-step authentication for instance. Try to generate an app password for logback and use it in place of your normal password in the config.
You can follow these instructions:
https://stackoverflow.com/a/25238515/1540818
My resources/application.conf looks like the following.
akka {
loggers = ["akka.event.slf4j.Slf4jLogger"]
loglevel = "DEBUG"
}
I'm creating an fat jar using $sbt assembly that contains this application.conf and deploying this jar to run my actors. Now can I change the log level (from DEBUG to INFO) of my program at runtime without brining down my actor system? If yes, how ?
My logback.xml looks like the following:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<target>System.out</target>
<encoder>
<pattern>%X{akkaTimestamp} %-5level[%thread] %logger{0} - %msg%n</pattern>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>logs/myjobs.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<fileNamePattern>logs/myjobs.%i.log.zip</fileNamePattern>
<minIndex>1</minIndex>
<maxIndex>5</maxIndex>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<maxFileSize>100MB</maxFileSize>
</triggeringPolicy>
<encoder>
<pattern>%date{yyyy-MM-dd} %X{akkaTimestamp} %-5level[%thread] %logger{1} - %msg%n</pattern>
</encoder>
</appender>
<logger name="akka" level="INFO" />
<root level="DEBUG">
<appender-ref ref="CONSOLE"/>
<appender-ref ref="FILE"/>
</root>
</configuration>
You can set the Akka loglevel using system.eventStream.setLogLevel() (within an Actor you would use context.system as the starting point).
I have developed a financial data distribution server with Akka, and I want to set logging level for the application. The documentation at akka.io is sketchy at the best; they say there is no more "logging" in Akka and logging is defined through event handlers now. There is also an example of event handler configuration, including logging level:
akka {
event-handlers = ["akka.event.EventHandler$DefaultListener"]
event-handler-level = "INFO"
}
I did that, but though akka.conf is successfully loaded, logging still appears to be at "DEBUG" level. What can be the problem there?
It appears that Akka uses slf4j/logback logging with default configuration. So the (never documented) solution would be to put e.g. the following logback.xml in your classpath:
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="false" debug="false">
<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>[%4p] [%d{ISO8601}] [%t] %c{1}: %m%n</pattern>
</encoder>
</appender>
<!-- you can also drop it completely -->
<logger name="se.scalablesolutions" level="DEBUG"/>
<root level="INFO">
<appender-ref ref="stdout"/>
</root>
</configuration>