Set logging level in Akka - scala

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>

Related

Is this logging blocking?

I have read that AkkaLogging is async and directly using slf4j is blocking. Consider the below code:
application.conf:
akka {
loggers = ["akka.event.slf4j.Slf4jLogger"]
}
logback.xml:
<configuration>
<appender name="InfoFile" class="ch.qos.logback.core.FileAppender">
<file>Testing.log</file>
<append>true</append>
<encoder>
<pattern>%msg%n</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="InfoFile" />
</root>
</configuration>
Log statements:
log.info("Sample logging")
Will this logging code be still blocking?If so, does it mean there is no way to achieve non blocking file logging in akka?
Akka logging is asynchronous.
But from this document, it seems "logging operations will block while the underlying infrastructure writes the log statements".
However, you can use nonblocking appenders AsyncAppender in your logback.xml to avoid this.

Logback SMTPAppender not working with gmail configuration

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

Change log level for phantom embedded Cassandra

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"/>

Chainsaw v2 SocketReceiver not working with log4j2 SocketAppender

I'm trying to use Chainsaw v2 from http://people.apache.org/~sdeboy
I don't want to use zero configuration. Just a simple socketAppender/SocketReceiver combo.
I'm using log4j2 with the following configuration
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" >
<Appenders>
<Console name="CONSOLE" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</Console>
<Socket name="SharathZeroConf" host="localhost" port="4445">
</Socket>
</Appenders>
<Loggers>
<Root level="debug">
<AppenderRef ref="SharathZeroConf" />
<AppenderRef ref="CONSOLE" />
</Root>
</Loggers>
</Configuration>
On ChainSaw, I'm selecting the option "Receive events from network" with port 4445.
However chainsaw doesnt log anything.
I've verified that the appender configuration is correct on log4j side by using the builtin socketserver
java -cp ~/.m2/reposiry/org/apache/logging/log4j/log4j-api/2.0.2/log4j-api-2.0.2.jar org.apache.logging.log4j.core.net.server.TcpSocketServer 4445
So the bug must be on chainsaw side. Any pointers #Scott ?
You're right, I got the same issue. I just tried with LogMX instead, and it works like a charm:
I just had to copy Log4j JARs in LogMX lib/ directory (i.e. log4j-api-2.xx.jar and log4j-core-2.xx.jar)

Log4J Appender UDP

I need to use log4j to append logs to a socket with UDP. However, I cannot find much on the internet about how to do so. In Log4J, the socketappender uses TCP. So I got log4j 2 beta, but I can't find any examples/documentation on how to use the socketappender, especially for UDP. I would really appreciate it if anyone could give me an example/show me how to use Log4j for UDP. Thanks.
I have been working with log4j 2.0-beta8 and got the UDP appender working with following log4j2.xml file (but note the 2 in the name of the file!):
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appenders>
<Socket name="UDP" host="myhostname.com" port="3333" protocol="UDP">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p %c{1} - %m%n"/>
</Socket>
</appenders>
<loggers>
<root level="info">
<appender-ref ref="UDP"/>
</root>
</loggers>
</configuration>