Reputation: 1013
I used a function where logger and parameters are initialized. Although I called this function LogMsg and newline in console, but it gives one single line in the text file. Why is that so?
String logmsg="Line1\n"+"Line2\n";
public static void LogMsg(Logger logger,String pathname,Level level,String logmsg){
//logger=Logger.getLogger("LogMsg");
FileHandler fh=null;
try {
fh = new FileHandler(pathname,300000,1,true);
fh.setFormatter(new SimpleFormatter());
LogRecord record1 = new LogRecord(level, logmsg);
logger.addHandler(fh);
logger.log(record1);
fh.close();
} catch (SecurityException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
System.out.println("Failed to log message due to lack of permissions.");
} catch (IOException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
System.out.println("Failed to log message");
}
}
Upvotes: 2
Views: 971
Reputation: 20330
Try
String logmsg="Line1\r\n"+"Line2\r\n";
or better still
logmsg = String.Concat("Line1",Environment.NewLine,"Line2",Environment.NewLine);
Or better still use a StringBuilder somethimng like
logmsg = CreateLogMessage(new string [] {"Line1", "Line2"});
public static CreateLogMessage(string[] argLines);
{
StringBuilder sb = new StringBuilder(argLines.Length);
foreach(String line in argLines)
{
sb.AppendLine(line);
}
return sb.ToString();
}
Environment.Newline will deal with os differences in terms of which end of line token is expected.
Upvotes: 2