0

i am using slf4j to save log.Here is the file log config log4j.properties log4j.rootLogger=ALL,CONSOLE,R

#Direct log messages to stdout
#log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
#log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
#log4j.appender.CONSOLE.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p  %c{1}:%L- %m%n

#Direct log messages to file
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=./log/KNetBigDataAnalytics.log
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p  %c{1}:%L- %m%n

I have a main form of swing application here is the code

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
    public FirstGui() {
        // textField.setText(dateFormat.format(date));
        getContentPane().setFont(new Font("Tahoma", Font.BOLD, 13));
        setResizable(false);
        setTitle("KNet Big Data Analytics Server");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 691, 460);
        getContentPane().setLayout(null);

        JTextArea textArea = new JTextArea();
        textArea.setBounds(151, 57, 513, 252);
        getContentPane().add(textArea);

        JButton btnStartServer = new JButton("Start");
        btnStartServer.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            //  MainClass.main();

            }
        });
        btnStartServer.setFont(new Font("Tahoma", Font.BOLD, 12));
        btnStartServer.setBounds(182, 363, 89, 23);
        getContentPane().add(btnStartServer);

        JButton btnStopServer = new JButton("Shutdown");

        btnStopServer.setFont(new Font("Tahoma", Font.BOLD, 12));
        btnStopServer.setBounds(313, 363, 101, 23);
        getContentPane().add(btnStopServer);

        JLabel lblNewLabel = new JLabel("Log Viewer");
        lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 12));
        lblNewLabel.setBackground(Color.RED);
        lblNewLabel.setBounds(151, 17, 83, 29);
        getContentPane().add(lblNewLabel);

        JSeparator separator = new JSeparator();
        separator.setBounds(140, 176, 1, 2);
        getContentPane().add(separator);

        textField = new JTextField();
        textField.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 11));
        textField.setBounds(281, 320, 141, 20);
        getContentPane().add(textField);
        textField.setColumns(10);
        new Timer(interval, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Calendar now = Calendar.getInstance();
                textField.setText(dateFormat.format(now.getTime()));
            }
        }).start();

what i want is how do i modify the properties log , in order to have log also in the JtextAreat in the main inteterface Please any help would be appreciated .

Maher HTB
  • 737
  • 3
  • 9
  • 23
  • 2
    Possible duplicate of [How to create my own Appender in log4j?](https://stackoverflow.com/questions/6072389/how-to-create-my-own-appender-in-log4j) – AxelH Jun 09 '17 at 11:02
  • From that, you can add a "Observer pattern" layer on that Appender to connect the JFrame to it, it will always log but will be received by the JFrame only if it is connected (observing) the appender. Each log would be received by the observer instance. – AxelH Jun 09 '17 at 11:04
  • 2
    You can also [redirect `System.out` and output it to a `JTextArea`](https://stackoverflow.com/questions/26270382/how-to-take-terminal-results-and-set-a-jtextarea-to-read-the-terminal/26270641#26270641) - did this kind of thing in the early days before logging systems were common – MadProgrammer Jun 09 '17 at 11:09
  • @MadProgrammer it will work if the log4j properties include a "ConsoleAppender", but indeed, that's the simplest solution when you don't want to use a logger API. – AxelH Jun 09 '17 at 11:11
  • @AxelH Agreed, which is why I commented, by I think the appender is a much more robust solution (although I've captured output from other libraries this way that didn't support or use the same logging system :P) – MadProgrammer Jun 09 '17 at 11:12
  • 1) `getContentPane().setLayout(null);` Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). 2) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Jun 09 '17 at 11:40

0 Answers0