email log file, generated by Java WebLogic Application Server - java

does this requirement conform to the J2EE Standards?
is there a easy way to implement this, log file gets generated by Log4J and in the end I will access the file system and email the whole file(s). can I access the file system?

Log4j has an email appender...
See: http://www.onjava.com/pub/a/onjava/2004/09/29/smtp-logging.html?page=2
(Also look on page 1)

with log4j, you can add an email appender to your configuration. You can declare the appender in your log4j.proeprties so:
log4j.appender.email=org.apache.log4j.net.SMTPAppender
log4j.appender.email.To= #recepient's email address
log4j.appender.email.From= #the sender's email address
log4j.appender.email.SMTPHost= #location of your smtp server
log4j.appender.email.Threshold=FATAL #the lowest log level on which the email is generated
log4j.appender.email.BufferSize=512
log4j.appender.email.Subject= #subject line of the email sent out
log4j.appender.email.layout=org.apache.log4j.PatternLayout
log4j.appender.email.layout.ConversionPattern=-[%d] %-4L %-5p %c %x - %m%n #message format
Something to remember: this appender will send an email on every log message that meets the threshold requirement so having a high threshold is recommended so your inbox doesn't get flooded with messages that are non critical.

Related

Logback will only log a message to syslog once

I've tried several examples of using logback to write to syslog, but the only that I've found that works is this JavaCodeGeeks example. It writes a message to syslog, but it only writes a message once no matter how many times I run the code. If I change the message it will write it to syslog, but only once.
I'm on Ubuntu 19.10. I've uncommented the following four lines from my /etc/rsyslog.conf and restarted :
# provides UDP syslog reception
module(load="imudp")
input(type="imudp" port="514")
# provides TCP syslog reception
module(load="imtcp")
input(type="imtcp" port="514")
The only change I made to the javacodegeeks code is to comment out the remote appender in logback.xml. It only logs to the localhost syslog.
What causes this weird behavior?
To log all messages you have to set
$RepeatedMsgReduction off
in /etc/rsyslog.conf and restart rsyslog.
https://www.rsyslog.com/doc/v8-stable/configuration/action/rsconf1_repeatedmsgreduction.html
The default was on in Ubuntu 19.10.

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.

Hiding or Encrypting Password in log4j.properties file SMTP Appender

I am using custom gmail smtp appendar for sending error logs from my gmail account.
Following the instructions: http://www.tgerm.com/2010/05/log4j-smtpappender-gmail-custom.html - Everything works great.
Except one thing:
In my log4j.properties: I don't want to type my password "log4j.appender.EMAIL.SMTPPassword=somepass" so that I can share this project with my team mates. Does anyone have a suggestion ?
Below is log4j.properties part:
log4j.appender.EMAIL=com.tgerm.log4j.appender.GmailSMTPAppender
log4j.appender.EMAIL.SMTPHost=smtp.gmail.com
log4j.appender.EMAIL.SMTPDebug=true
log4j.appender.EMAIL.From=from#gmail.com
log4j.appender.EMAIL.To=to#tgerm.com
log4j.appender.EMAIL.SMTPUsername=smtpuser#gmail.com
log4j.appender.EMAIL.SMTPPassword=somepass //this is the problematic part
log4j.appender.EMAIL.Subject=Email Notification from Gmail SMTP Appender
log4j.appender.EMAIL.cc=cc#gmail.com
log4j.appender.EMAIL.layout=org.apache.log4j.PatternLayout
log4j.appender.EMAIL.layout.ConversionPattern=%p %t %c - %m%n
log4j.appender.EMAIL.BufferSize=1
For starters: don't use your personal email address - create a new account to be shared with your team members.
If you still want to encrypt, put the encrypted information in a separate properties or xml file, write code to encrypt and decrypt it, and configure that portion of log4j programatically.
(And, as gdt says below, remember, if the application can decrypt the password, others can too. There is no 100% safe solution. Protecting the file access permissions is often more effective than encrypting.)
Create a service email account (not personal) which can be shared.
To hide password, specify it in properies file, but after first run, read it, encrypt and write back to the properties file.

Categories

Resources