altgr doesn't work with jnativehook? - java

It's just for testing purposes.
I tried to change some parts of the code and read also the api-docs.
On begin I added "typedC += e.getKeyChar();" and it looked like to log the altgr-keys, but later not anymore.
How to change it to let work altgr-keys ?
Thanks
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import org.jnativehook.GlobalScreen;
import org.jnativehook.NativeHookException;
import org.jnativehook.keyboard.NativeKeyEvent;
import org.jnativehook.keyboard.NativeKeyListener;
public class JkL implements NativeKeyListener {
private String typedC = "";
private String typedC1 = "";
private final JFileChooser fc = new JFileChooser();
private File direc;
private void openFchooser1() throws FileNotFoundException,
InterruptedException, IOException, Exception {
Thread.sleep(2000);
int returnVal = fc.showDialog(null, "Choose a Logfile");
if(returnVal == JFileChooser.APPROVE_OPTION) {
direc = fc.getSelectedFile();
}
/* Construct the example object and initialze native hook. */
GlobalScreen.addNativeKeyListener(new Kl());
try {
/* Register jNativeHook */
GlobalScreen.registerNativeHook();
} catch (NativeHookException ex) {
/* Its error */
System.err.println("There was a problem registering the native
hook.");
System.err.println(ex.getMessage());
System.exit(1);
}
// Clear previous logging configurations.
LogManager.getLogManager().reset();
// Get the logger for "org.jnativehook" and set the level to off.
Logger logger =
Logger.getLogger(GlobalScreen.class.getPackage().getName());
logger.setLevel(Level.OFF);
}
#Override
public void nativeKeyPressed(NativeKeyEvent nke) {
throw new UnsupportedOperationException("Not supported yet."); //To
change body of generated methods, choose Tools | Templates.
}
#Override
public void nativeKeyReleased(NativeKeyEvent nke) {
throw new UnsupportedOperationException("Not supported yet."); //To
change body of generated methods, choose Tools | Templates.
}
#Override
public void nativeKeyTyped(NativeKeyEvent nke) {
throw new UnsupportedOperationException("Not supported yet."); //To
change body of generated methods, choose Tools | Templates.
}
class Kl extends JkL {
/* Key Pressed */
#Override
public void nativeKeyPressed(NativeKeyEvent e) {
//typedC += NativeKeyEvent.getKeyText(e.getKeyCode());
typedC += e.getRawCode();
typedC += e.getKeyChar();
typedC += e.getKeyCode();
try {
writeToFile(typedC);
} catch (IOException ex) {
Logger.getLogger(JkL.class.getName()).log(Level.SEVERE, null,
ex);
}
/* Terminate program when one press ESCAPE */
if (e.getKeyCode() == NativeKeyEvent.VC_F12) {
try {
GlobalScreen.unregisterNativeHook();
} catch (NativeHookException ex) {
Logger.getLogger(JkL.class.getName()).log(Level.SEVERE,
null, ex);
}
}
}
#Override
public void nativeKeyReleased(NativeKeyEvent e) {
//System.out.println("Key Released: " +
//NativeKeyEvent.getKeyText(e.getKeyCode()));
}
/* I can't find any output from this call */
#Override
public void nativeKeyTyped(NativeKeyEvent e) {
//typedC1 += NativeKeyEvent.getKeyText(e.getKeyCode());
typedC1 += e.getRawCode();
typedC1 += e.getKeyChar();
typedC1 += e.getKeyCode();
try {
writeToFile(typedC1);
} catch (IOException ex) {
Logger.getLogger(JkL.class.getName()).log(Level.SEVERE, null,
ex);
}
}
}
private void writeToFile(String ln) throws IOException {
//System.out.println(direc);
FileWriter fw = new FileWriter(direc);
try (BufferedWriter bw = new BufferedWriter(fw)) {
bw.write(ln);
bw.newLine();
bw.flush();
}
}
public static void main(String[] args) throws
IOException,InterruptedException, Exception {
new JkL().openFchooser1();
}
}

What operating system? There are some known issues with 2.0 on Linux but they will be fixed in 2.1. If there are any other issues, you will probably need to report a bug. Please include your exact keyboard layout, model and language. Also provided detailed information about what key is pressed, the observed behavior and the expected behavior. I only know how to use a US keyboard, so I am not sure how all of the Alt-Gr language/region specific keys, masks and locks are suppose to work.

Related

RandomAccessFile doesn't work with Minecraft Forge

I'm working at the moment on a Mod for Minecraft with a dedicated Gui system written in C++ and Qt5. I let my GUI and Minecraft communicate through a named pipe, but I have there a small problem. I can read and write with a simple Java and C++(Qt) program into the pipe. But when I create a new instance of my Pipeendpoint class in post init of Minecraft Forge it can't read anything from the Pipe. In a standalone system, it can read stuff.
Not working Forge Implementation:
package de.CoderDE.CodersAnimationEditor;
import java.io.FileNotFoundException;
import de.CoderDE.CodersAnimationEditor.Pipe.PipeEndpoint;
import net.minecraft.client.Minecraft;
import net.minecraftforge.fml.client.registry.ClientRegistry;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
#SideOnly(Side.CLIENT)
public class ClientProxy extends CommonProxy {
static PipeEndpoint pendpoint;
#Override
public void preInit(FMLPreInitializationEvent e) {
super.preInit(e);
}
#Override
public void init(FMLInitializationEvent e) {
super.init(e);
}
#Override
public void postInit(FMLPostInitializationEvent e) {
super.postInit(e);
try {
pendpoint = new PipeEndpoint();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
try {
Thread.sleep(10000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
}
Working standalone implementation:
import java.io.FileNotFoundException;
import de.CoderDE.CodersAnimationEditor.Pipe.PipeEndpoint;
public class Main {
static PipeEndpoint pipe;
public static void main(String[] args) {
try {
pipe = new PipeEndpoint();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
And the important PipeEndpoint class:
package de.CoderDE.CodersAnimationEditor.Pipe;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
public class PipeEndpoint {
private Thread reciever;
private RandomAccessFile pipe;
public PipeEndpoint() throws FileNotFoundException {
pipe = new RandomAccessFile("\\\\.\\pipe\\CodersAnimationEditor", "rw");
reciever = new Thread(new PipeEndpointReciever());
reciever.start();
}
private class PipeEndpointReciever implements Runnable {
#Override
public void run() {
try {
while (true) {
System.out.print((char)pipe.read());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
And with "can't read anything" I mean that it never returns from pipe.read().
Oh, and the Java application starts after the C++(Qt) LocalServer started listening and waits for a new connection.

Java read Windows Key State

I want to write a program that plays a sound everytime, a button on the Keyboard is pressed. (Even, if the program isn´t in focus)
I am using JNativeHook, but when i want to add a Key Listener, i get an error beacuse the method .getInstance isn´t contained in Global Screen. :(
Anyone got an idea?
My Code:
package test1;
import org.jnativehook.GlobalScreen;
import org.jnativehook.NativeHookException;
import org.jnativehook.keyboard.NativeKeyEvent;
import org.jnativehook.keyboard.NativeKeyListener;
public class jNativeHookExample implements NativeKeyListener {
public void nativeKeyPressed(NativeKeyEvent e) {
System.out.println(NativeKeyEvent.getKeyText(e.getKeyCode()));
if (e.getKeyCode() == NativeKeyEvent.VC_F9) {
//play sound;
}
}
public void nativeKeyReleased(NativeKeyEvent e) {
System.out.println("Key Released: " + NativeKeyEvent.getKeyText(e.getKeyCode()));
}
public void nativeKeyTyped(NativeKeyEvent e) {
System.out.println("Key Typed: " + NativeKeyEvent.getKeyText(e.getKeyCode()));
}
public static void main(String[] args) {
try {
/* Register jNativeHook */
GlobalScreen.registerNativeHook();
} catch (NativeHookException ex) {
/* Its error */
System.err.println("There was a problem registering the native hook.");
System.err.println(ex.getMessage());
System.exit(1);
}
GlobalScreen.getInstance().addNativeKeyListener(new jNativeHookExample());
}
}
This code is probably ok for the version 1.1 of jnativehook.
Starting from the version 2.0 the GlobalScreen class has no getInstance() method, and addNativeKeyListener() is now static, so it should be called directly on the GlobalScreen:
GlobalScreen.addNativeKeyListener(new jNativeHookExample());

Java socket writeObject stucks

I am trying to create some local chat software.
Everything is working fine but just sticks on one point;
when a user clicks on a button my program gets stuck.
I wrote a modified class which behaves for both the server and the client.
Below is my code:
package connections;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.nio.channels.SocketChannel;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import ui.ChatWindow;
public class Chat{
private SocketChannel socketChannel;
private ConnectionType connectionType;
private ObjectOutputStream out;
private ObjectInputStream in;
private String strClientName;
private ChatWindow chatWindow;
private String strMessage;
public Chat(SocketChannel socketChannel, ConnectionType connectionType) {
this.socketChannel = socketChannel;
this.connectionType = connectionType;
init();
}
private void init() {
new Thread(this::initThread).start();
}
private void initThread() {
try {
out = new ObjectOutputStream(socketChannel.socket().getOutputStream());
in = new ObjectInputStream(socketChannel.socket().getInputStream());
if (connectionType == ConnectionType.SERVER) {
getClientName();
sendMyName();
} else {
sendMyName();
getClientName();
}
chatWindow = new ChatWindow();
chatWindow.btnSend.addActionListener(this::eventBtnSend);
chatWindow.setTitle(strClientName);
chatWindow.setSize(600, 400);
chatWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
if (connectionType == ConnectionType.CLIENT) {
chatWindow.setVisible(true);
chatWindow.requestFocus();
}
System.out.println("here");
beginChat();
System.out.println("thread ends");
} catch (IOException ex) {
Logger.getLogger(Chat.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void getClientName() {
try {
strClientName = (String) in.readObject();
} catch (IOException | ClassNotFoundException ex) {
Logger.getLogger(Status.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void sendMyName() {
try {
out.writeObject(main.Config.myName);
out.flush();
} catch (IOException ex) {
Logger.getLogger(Status.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void beginChat() {
while (true) {
try {
waitForMessage();
} catch (IOException | ClassNotFoundException ex) {
Logger.getLogger(Chat.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
private void waitForMessage() throws IOException, ClassNotFoundException {
System.out.println("waiting for Message : " + connectionType);
strMessage = (String) in.readObject();
System.out.println("Message received");
chatWindow.txtDisplay.append(strClientName + " : " + strMessage + "\n");
if(chatWindow.isVisible() == false){
chatWindow.setVisible(true);
}
}
private void sendMessage(String strMessage) {
try {
out.writeObject(strMessage);
out.flush();
} catch (IOException ex) {
Logger.getLogger(Chat.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void eventBtnSend(ActionEvent ae){
System.out.println("a");
sendMessage(chatWindow.txtMessage.getText());
System.out.println("b");
chatWindow.txtDisplay.append("Me : " + chatWindow.txtMessage.getText() + "\n");
System.out.println("c");
chatWindow.txtMessage.setText("");
}
}
When the server and client connects I can see this output:
here
waiting for Message : SERVER
here
waiting for Message : CLIENT
But when I click on the send button my program becomes stuck.
After clicking the button I can see:
a on console.
The word is 'blocks'. It's blocking I/O. It blocks.
Don't perform blocking operations on the event thread. All the networking code should run on separate threads. That includes readObject() and writeObject(), and flush(), and creating the streams too.

How to write logs in text file when using java.util.logging.Logger

I have a situation in which I want to write all logs created by me into a text file.
We are using java.util.logging.Logger API to generate the logs.
I tried:
private static Logger logger = Logger.getLogger(className.class.getName());
FileHandler fh;
fh = new FileHandler("C:/className.log");
logger.addHandler(fh);
But still getting my logs on console only....
Try this sample. It works for me.
public static void main(String[] args) {
Logger logger = Logger.getLogger("MyLog");
FileHandler fh;
try {
// This block configure the logger with handler and formatter
fh = new FileHandler("C:/temp/test/MyLogFile.log");
logger.addHandler(fh);
SimpleFormatter formatter = new SimpleFormatter();
fh.setFormatter(formatter);
// the following statement is used to log any messages
logger.info("My first log");
} catch (SecurityException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
logger.info("Hi How r u?");
}
Produces the output at MyLogFile.log
Apr 2, 2013 9:57:08 AM testing.MyLogger main
INFO: My first log
Apr 2, 2013 9:57:08 AM testing.MyLogger main
INFO: Hi How r u?
Edit:
To remove the console handler, use
logger.setUseParentHandlers(false);
since the ConsoleHandler is registered with the parent logger from which all the loggers derive.
Firstly, where did you define your logger and from what class\method trying to call it? There is a working example, fresh baked:
public class LoggingTester {
private final Logger logger = Logger.getLogger(LoggingTester.class
.getName());
private FileHandler fh = null;
public LoggingTester() {
//just to make our log file nicer :)
SimpleDateFormat format = new SimpleDateFormat("M-d_HHmmss");
try {
fh = new FileHandler("C:/temp/test/MyLogFile_"
+ format.format(Calendar.getInstance().getTime()) + ".log");
} catch (Exception e) {
e.printStackTrace();
}
fh.setFormatter(new SimpleFormatter());
logger.addHandler(fh);
}
public void doLogging() {
logger.info("info msg");
logger.severe("error message");
logger.fine("fine message"); //won't show because to high level of logging
}
}
In your code you forgot to define the formatter, if you need simple one you can do it as I mentioned above, but there is another option, you can format it by yourself, there is an example (just insert it instead of this line fh.setFormatter(new SimpleFormatter()) following code):
fh.setFormatter(new Formatter() {
#Override
public String format(LogRecord record) {
SimpleDateFormat logTime = new SimpleDateFormat("MM-dd-yyyy HH:mm:ss");
Calendar cal = new GregorianCalendar();
cal.setTimeInMillis(record.getMillis());
return record.getLevel()
+ logTime.format(cal.getTime())
+ " || "
+ record.getSourceClassName().substring(
record.getSourceClassName().lastIndexOf(".")+1,
record.getSourceClassName().length())
+ "."
+ record.getSourceMethodName()
+ "() : "
+ record.getMessage() + "\n";
}
});
Or any other modification whatever you like. Hope it helps.
Location of log file can be control through logging.properties file. And it can be passed as JVM parameter ex : java -Djava.util.logging.config.file=/scratch/user/config/logging.properties
Details: https://docs.oracle.com/cd/E23549_01/doc.1111/e14568/handler.htm
Configuring the File handler
To send logs to a file, add FileHandler to the handlers property in the logging.properties file. This will enable file logging globally.
handlers= java.util.logging.FileHandler Configure the handler by setting the following properties:
java.util.logging.FileHandler.pattern=<home directory>/logs/oaam.log
java.util.logging.FileHandler.limit=50000
java.util.logging.FileHandler.count=1
java.util.logging.FileHandler.formatter=java.util.logging.SimpleFormatter
java.util.logging.FileHandler.pattern specifies the location and pattern of the output file. The default setting is your home directory.
java.util.logging.FileHandler.limit specifies, in bytes, the maximum amount that the logger writes to any one file.
java.util.logging.FileHandler.count specifies how many output files to cycle through.
java.util.logging.FileHandler.formatter specifies the java.util.logging formatter class that the file handler class uses to format the log messages. SimpleFormatter writes brief "human-readable" summaries of log records.
To instruct java to use this configuration file instead of $JDK_HOME/jre/lib/logging.properties:
java -Djava.util.logging.config.file=/scratch/user/config/logging.properties
A good library available named log4j for Java.
This will provide numerous feature. Go through link and you will find your solution.
Maybe this is what you need...
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
/**
* LogToFile class
* This class is intended to be use with the default logging class of java
* It save the log in an XML file and display a friendly message to the user
* #author Ibrabel <ibrabel#gmail.com>
*/
public class LogToFile {
protected static final Logger logger=Logger.getLogger("MYLOG");
/**
* log Method
* enable to log all exceptions to a file and display user message on demand
* #param ex
* #param level
* #param msg
*/
public static void log(Exception ex, String level, String msg){
FileHandler fh = null;
try {
fh = new FileHandler("log.xml",true);
logger.addHandler(fh);
switch (level) {
case "severe":
logger.log(Level.SEVERE, msg, ex);
if(!msg.equals(""))
JOptionPane.showMessageDialog(null,msg,
"Error", JOptionPane.ERROR_MESSAGE);
break;
case "warning":
logger.log(Level.WARNING, msg, ex);
if(!msg.equals(""))
JOptionPane.showMessageDialog(null,msg,
"Warning", JOptionPane.WARNING_MESSAGE);
break;
case "info":
logger.log(Level.INFO, msg, ex);
if(!msg.equals(""))
JOptionPane.showMessageDialog(null,msg,
"Info", JOptionPane.INFORMATION_MESSAGE);
break;
case "config":
logger.log(Level.CONFIG, msg, ex);
break;
case "fine":
logger.log(Level.FINE, msg, ex);
break;
case "finer":
logger.log(Level.FINER, msg, ex);
break;
case "finest":
logger.log(Level.FINEST, msg, ex);
break;
default:
logger.log(Level.CONFIG, msg, ex);
break;
}
} catch (IOException | SecurityException ex1) {
logger.log(Level.SEVERE, null, ex1);
} finally{
if(fh!=null)fh.close();
}
}
public static void main(String[] args) {
/*
Create simple frame for the example
*/
JFrame myFrame = new JFrame();
myFrame.setTitle("LogToFileExample");
myFrame.setSize(300, 100);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setLocationRelativeTo(null);
JPanel pan = new JPanel();
JButton severe = new JButton("severe");
pan.add(severe);
JButton warning = new JButton("warning");
pan.add(warning);
JButton info = new JButton("info");
pan.add(info);
/*
Create an exception on click to use the LogToFile class
*/
severe.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent ae) {
int j = 20, i = 0;
try {
System.out.println(j/i);
} catch (ArithmeticException ex) {
log(ex,"severe","You can't divide anything by zero");
}
}
});
warning.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent ae) {
int j = 20, i = 0;
try {
System.out.println(j/i);
} catch (ArithmeticException ex) {
log(ex,"warning","You can't divide anything by zero");
}
}
});
info.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent ae) {
int j = 20, i = 0;
try {
System.out.println(j/i);
} catch (ArithmeticException ex) {
log(ex,"info","You can't divide anything by zero");
}
}
});
/*
Add the JPanel to the JFrame and set the JFrame visible
*/
myFrame.setContentPane(pan);
myFrame.setVisible(true);
}
}
import java.io.IOException;
import org.apache.log4j.Appender;
import org.apache.log4j.FileAppender;
import org.apache.log4j.Logger;
import org.apache.log4j.SimpleLayout;
/**
* #author Kiran
*
*/
public class MyLogger {
public MyLogger() {
}
public static void main(String[] args) {
Logger logger = Logger.getLogger("MyLog");
Appender fh = null;
try {
fh = new FileAppender(new SimpleLayout(), "MyLogFile.log");
logger.addAppender(fh);
fh.setLayout(new SimpleLayout());
logger.info("My first log");
} catch (SecurityException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
logger.info("Hi How r u?");
}
}
int SIZE = "<intialize-here>"
int ROTATIONCOUNT = "<intialize-here>"
Handler handler = new FileHandler("test.log", SIZE, LOG_ROTATIONCOUNT);
logger.addHandler(handler); // for your code..
// you can also set logging levels
Logger.getLogger(this.getClass().getName()).log(Level.[...]).addHandler(handler);
Hope people find this helpful
public static void writeLog(String info) {
String filename = "activity.log";
String FILENAME = "C:\\testing\\" + filename;
BufferedWriter bw = null;
FileWriter fw = null;
try {
fw = new FileWriter(FILENAME, true);
bw = new BufferedWriter(fw);
bw.write(info);
bw.write("\n");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bw != null)
bw.close();
if (fw != null)
fw.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
Here is my logging class based on the accepted answer:
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.*;
public class ErrorLogger
{
private Logger logger;
public ErrorLogger()
{
logger = Logger.getAnonymousLogger();
configure();
}
private void configure()
{
try
{
String logsDirectoryFolder = "logs";
Files.createDirectories(Paths.get(logsDirectoryFolder));
FileHandler fileHandler = new FileHandler(logsDirectoryFolder + File.separator + getCurrentTimeString() + ".log");
logger.addHandler(fileHandler);
SimpleFormatter formatter = new SimpleFormatter();
fileHandler.setFormatter(formatter);
} catch (IOException exception)
{
exception.printStackTrace();
}
addCloseHandlersShutdownHook();
}
private void addCloseHandlersShutdownHook()
{
Runtime.getRuntime().addShutdownHook(new Thread(() ->
{
// Close all handlers to get rid of empty .LCK files
for (Handler handler : logger.getHandlers())
{
handler.close();
}
}));
}
private String getCurrentTimeString()
{
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
return dateFormat.format(new Date());
}
public void log(Exception exception)
{
logger.log(Level.SEVERE, "", exception);
}
}
Here is an example of how to overwrite Logger configuration from the code. Does not require external configuration file ..
FileLoggerTest.java:
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.Logger;
public class FileLoggerTest {
public static void main(String[] args) {
try {
String h = MyLogHandler.class.getCanonicalName();
StringBuilder sb = new StringBuilder();
sb.append(".level=ALL\n");
sb.append("handlers=").append(h).append('\n');
LogManager.getLogManager().readConfiguration(new ByteArrayInputStream(sb.toString().getBytes("UTF-8")));
} catch (IOException | SecurityException ex) {
// Do something about it
}
Logger.getGlobal().severe("Global SEVERE log entry");
Logger.getLogger(FileLoggerTest.class.getName()).log(Level.SEVERE, "This is a SEVERE log entry");
Logger.getLogger("SomeName").log(Level.WARNING, "This is a WARNING log entry");
Logger.getLogger("AnotherName").log(Level.INFO, "This is an INFO log entry");
Logger.getLogger("SameName").log(Level.CONFIG, "This is an CONFIG log entry");
Logger.getLogger("SameName").log(Level.FINE, "This is an FINE log entry");
Logger.getLogger("SameName").log(Level.FINEST, "This is an FINEST log entry");
Logger.getLogger("SameName").log(Level.FINER, "This is an FINER log entry");
Logger.getLogger("SameName").log(Level.ALL, "This is an ALL log entry");
}
}
MyLogHandler.java
import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.SimpleFormatter;
public final class MyLogHandler extends FileHandler {
public MyLogHandler() throws IOException, SecurityException {
super("/tmp/path-to-log.log");
setFormatter(new SimpleFormatter());
setLevel(Level.ALL);
}
#Override
public void publish(LogRecord record) {
System.out.println("Some additional logic");
super.publish(record);
}
}

monitor multiple log file simultaneously

I have made a program that continuously monitors a log file. But I don't know how to monitor multiple log files. This is what I did to monitor single file. What changes should I make in the following code so that it monitors multiple files also?
package com.read;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class FileWatcherTest {
public static void main(String args[]) {
final File fileName = new File("D:/logs/myFile.log");
// monitor a single file
TimerTask fileWatcherTask = new FileWatcher(fileName) {
long addFileLen = fileName.length();
FileChannel channel;
FileLock lock;
String a = "";
String b = "";
#Override
protected void onChange(File file) {
RandomAccessFile access = null;
try {
access = new RandomAccessFile(file, "rw");
channel = access.getChannel();
lock = channel.lock();
if (file.length() < addFileLen) {
access.seek(file.length());
} else {
access.seek(addFileLen);
}
} catch (Exception e) {
e.printStackTrace();
}
String line = null;
try {
while ((line = access.readLine()) != null) {
System.out.println(line);
}
addFileLen = file.length();
} catch (IOException ex) {
Logger.getLogger(FileWatcherTest.class.getName()).log(
Level.SEVERE, null, ex);
}
try {
lock.release();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} // Close the file
try {
channel.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
Timer timer = new Timer();
// repeat the check every second
timer.schedule(fileWatcherTask, new Date(), 1000);
}
}
package com.read;
import java.util.*;
import java.io.*;
public abstract class FileWatcher extends TimerTask {
private long timeStamp;
private File file;
static String s;
public FileWatcher(File file) {
this.file = file;
this.timeStamp = file.lastModified();
}
public final void run() {
long timeStamp = file.lastModified();
if (this.timeStamp != timeStamp) {
this.timeStamp = timeStamp;
onChange(file);
}
}
protected abstract void onChange(File file);
}
You should use threads. Here's a good tutorial:
http://docs.oracle.com/javase/tutorial/essential/concurrency/
You would do something like:
public class FileWatcherTest {
public static void main(String args[]) {
(new Thread(new FileWatcherRunnable("first.log"))).start();
(new Thread(new FileWatcherRunnable("second.log"))).start();
}
private static class FileWatcherRunnable implements Runnable {
private String logFilePath;
// you should inject the file path of the log file to watch
public FileWatcherRunnable(String logFilePath) {
this.logFilePath = logFilePath;
}
public void run() {
// your code from main goes in here
}
}
}

Categories