Log inputstream - java

Do You know any neat solution to log an InputStream, can by into file. InputStream from a Process never ends and generate some stream from time to time.
Thanks
Bartek

If you want to log never-ending stream transparently - I believe you need a Tee filter - see answers in this question. E.g. you can use Commons IO.

For you case is the most sutable will be log4j.
But you can redirect System.err and System.out into console or smth else and it'll be write all errors in specific stream.
upd:
# Root logger option log4j.rootLogger=INFO, file, stdout
# Direct log messages to a log file log4j.appender.file=org.apache.log4j.RollingFileAppender log4j.appender.file.File=C:\\loging.log log4j.appender.file.MaxFileSize=1MB log4j.appender.file.MaxBackupIndex=1 log4j.appender.file.layout=org.apache.log4j.PatternLayout log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L
- %m%n
# Direct log messages to stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target=System.out log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

Related

How to create log4j for send email notification if same error occured more than 5 times?

Please find my log4j.properties given below.
log4j.debug=TRUE
# Root logger option
log4j.rootLogger=info,stdout,file
# Redirect log messages to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
# LOG4J daily rolling log files configuration
log4j.appender.file=org.apache.log4j.rolling.RollingFileAppender
log4j.appender.file.rollingPolicy=org.apache.log4j.rolling.TimeBasedRollingPolicy
log4j.appender.file.rollingPolicy.fileNamePattern=%d{yyyy_MM_dd}.log
log4j.appender.file.MaxFileSize=5MB
log4j.appender.file.MaxBackupIndex=5
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
#CONFIGURE SMTP
#CONFIGURE SMTP
log4j.rootLogger= ERROR,email
log4j.appender.email=org.apache.log4j.net.SMTPAppender
#log4j.appender.email.transport.protocol=smtp
log4j.appender.email.SMTPProtocol=smtps
log4j.appender.email.SMTPHost=smtp.gmail.com
log4j.appender.email.SMTPPort=465
log4j.appender.mail.smtp.starttls.enable=false
log4j.appender.email.SMTPUsername=abc#gmail.com
log4j.appender.email.SMTPPassword=*****
log4j.appender.email.threshold=error
log4j.appender.email.From=abc#gmail.com
log4j.appender.email.To=abc#gmail.com
log4j.appender.email.BufferSize=1
#log4j.appender.email.EvaluatorClass=TriggerLogEvent
log4j.appender.email.Subject=Logging Message via Gmail
log4j.appender.email.layout=org.apache.log4j.PatternLayout
log4j.appender.email.layout.ConversionPattern=%m
By using this, if error occurred,then a mail send to the given Gmail account. But I need to send mail only if same error occurred more than 5 times? What are the changes that i made in the log4j?
Check out this article on DZone that uses an open source library called Stat4j. The example stat4j.properties linked in the article is pretty self-explanatory and looks like what you're looking for.

Log4j2 custom fields Socket appender

I'm currently sending information from my Java application to ELK using Log4j2 and socketAppender. I'd like to know if it's possible to add extra information to this message sent to ELK with things like, application name, original request, and stuff like that:
My configuration right now looks like this:
Configuration:
name: Remote
Appenders:
Console:
name: Console
target: SYSTEM_OUT
PatternLayout:
Pattern: "%d{yyyy-MM-dd HH:mm:ss} %-5p %c{10}:%L - %m%n"
Socket:
name: Elk
port: 7000
host: elk
reconnectionDelayMillis: 10000
SerializedLayout: []
Any clue or documentation about how to do it?
You can add key-value pairs to the ThreadContext. These will be included in the LogEvent. SerializedLayout will also include this so you should be able to receive it on the other end of the socket.

How can I use log4j to log why RMI is failing

I am currently using log4j to log on both server and client side processes.
But I am having an rmi failure.
I would like to know if the problem is on the client side or the server side.
How can I use log4j to show me the rmi activity on both the client and server?
Note that I don't see any rmi logging.
My log4j file contains this:
log4j.rootLogger=DEBUG, file, Socket_Logger, CONSOLE
log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.File=C:/log/RDS.log
log4j.appender.file.Append=true
log4j.appender.DatePattern='.'yyyy-MM-dd
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d %-5p (%t)[%c] %m%n
log4j.appender.file.Threshold=DEBUG
The server log4j log shows things like:
at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(RMIDirectSocketFactory.java:40)
at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(RMIMasterSocketFactory.java:148)
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:613)
But the Client side log doesn't show anything
Log4J has nothing to do with it. RMI uses Java logging. What you need to do is enable the client-side logging you need, via the RMI system logLevel properties.

Configure log4j to send email

In log4j.properties I have
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.EnhancedPatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} %-5p [correlation-ids:%X{X-CAAP-Correlation-ID},user:%X{X-CAAP-User}] [%t] (%C:%L) %X{camel.routeId}- %m%n
log4j.logger.com.lacapitale=INFO
log4j.logger.org.apache.cxf=INFO
log4j.logger.org.apache.camel=DEBUG
log4j.logger.org.springframework=INFO
log4j.appender.SMTP=org.apache.log4j.net.SMTPAppender
log4j.appender.SMTP.BufferSize=1
log4j.appender.SMTP.From=noreply#mycompany.com
log4j.appender.SMTP.SMTPHost=localhost
log4j.appender.SMTP.Subject=[{{appid}}] - Erreur technique
log4j.appender.SMTP.Threshold=INFO
log4j.appender.SMTP.To=me#mycompany.com
log4j.appender.SMTP.layout=org.apache.log4j.PatternLayout
log4j.appender.SMTP.layout.conversionPattern=%d{ISO8601} %-5p %m%n
log4j.rootLogger=INFO, CONSOLE
log4j.logger.email=INFO, SMTP
But, I never get any email when I log an info.
Is there anything wrong in my config?
Is it because I deployed the app in Eclipse embedded Tomcat? Does Tomcat always include a SMTP server?
Tomcat does not include an SMTP server. You will need to make sure the SMTPHost you have defined has an MTA installed, running, and accepting connections.

Change Log4J's Threshold dynamically through user input

I'm writing a simple console application which sends and receives data to/from a socket via user commands, such as "send hello" -> receives "hello" from socket. One of the available commands should be logLevel (ALL|WARN|DEBUG|etc.), which enables the user to set the log level of the Log4J logger which I'M using throughout the application.
Right now, I specified two Appenders in my properties file:
# Root logger option
log4j.rootLogger=INFO, file, stdout
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=./logs/client.log
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.MaxBackupIndex=1
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
# info and above messages are printed to stdout
log4j.appender.stdout.Threshold=INFO
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
My goal here is to send all log entries from any level to the logfile, but only the ones that match the specified log level currently set by the user to stdout. A sample workflow could look like this:
> INFO: current log level is INFO and above.
> send hello
> INFO: sending hello to server
> INFO: receiving "hello" from server
> logLevel WARN
> INFO: log level set to warn
> send hello
> # no info here, but its still sent to the logfile
My goal here is to send all log entries from any level to the logfile, but only the ones that match the specified log level currently set by the user to stdout.
So essentially you want to change the threshold on the stdout appender programmatically. This is easy enough to do, the wrinkle is that the threshold property is part of the AppenderSkeleton abstract class (which ConsoleAppender extends) rather than the Appender interface itself.
((AppenderSkeleton)Logger.getRootLogger().getAppender("stdout"))
.setThreshold(Level.toLevel(levelNameProvidedByUser));
If you really do want all log messages to go to the file then you should probably configure the root logger level to ALL rather than INFO in your log4j.properties, and rely on the threshold to filter stdout.
My requirement is to add specific logs to one of my log file. I have two log files. One for normal case and other I have to add only specific logs. Means I have to make threshold off by default. Once requires I have to make threshold INFO and add then logs to my log file and then again make threshold OFF.
This is my code of java
org.apache.log4j.Logger logger = org.apache.log4j.Logger.getRootLogger();
Appender appender = logger.getAppender("logFileName");
((AppenderSkeleton)appender).setThreshold(Level.INFO);
logger.info(myString.toString());
((AppenderSkeleton)appender).setThreshold(Level.OFF);
So only myString is log to my logFileName file.

Categories

Resources