Jump to content

Colorful Log [Question]


Yusi

Recommended Posts

16 hours ago, Yusi said:

Hello is colorful log possible in osbot? If it is can you tell me how?

Unfortunately this isn't available with OSBot. You would have to create a custom logger.

Here is a good link on how you can modify the logger for color: https://www.baeldung.com/java-log-console-in-color

Edited by ExtraBotz
  • Like 1
Link to comment
Share on other sites

here is a custom one I made ages ago. Feel free to edit it.

 

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;

import javax.swing.DefaultListCellRenderer;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;

public class UltimateLogger {

   public UltimateLogger() {
      createLogger();
   }

   public void close() {
      loggerFrame.setVisible(false);
      loggerFrame.dispose();
   }

   public void open() {
      loggerFrame.setVisible(true);
   }

   private void createLogger() {
      JTabbedPane loggerTab = getTabbedPane();
      loggerFrame = getFrame();
      loggerFrame.add(loggerTab);
      open();
   }

   private JFrame getFrame() {
      JFrame frame = new JFrame("Ultimate Logger");
      final Dimension frameDimention = new Dimension(650, 350);
      frame.setSize(frameDimention);
      frame.setMinimumSize(frameDimention);
      return frame;
   }

   private JTabbedPane getTabbedPane() {
      JPanel scrollPanel = new JPanel();
      scrollPane = new JScrollPane(scrollPanel);
      list = new JList<String>(listModel);
      scrollPanel.add(list);
      scrollPanel.setLayout(new GridLayout(1, 0));
      JTabbedPane tab = new JTabbedPane();
      tab.addTab("Ultimate Bot Debug", scrollPane);
      return tab;
   }

   public void log(String loggedString) {
      listModel.addElement(getOkHeader() + loggedString);
      list.setCellRenderer(new CellRender());
      setMaximumScroll();
   }

   public void logError(String loggedString) {
      listModel.addElement(getErrorHeader() + loggedString + "\n");
      list.setCellRenderer(new CellRender());
      setMaximumScroll();
   }

   public void logWarning(String loggedString) {
      listModel.addElement(getWarningHeader() + loggedString + "\n");
      list.setCellRenderer(new CellRender());
      setMaximumScroll();
   }

   private void setMaximumScroll() {
      JScrollBar vertical = scrollPane.getVerticalScrollBar();
      vertical.setValue(vertical.getMaximum());
   }

   private String getOkHeader() {
      return "[OKAY][UltimateLogger][" + getTimeStamp() + "]: ";
   }

   private String getErrorHeader() {
      return "[ERROR][UltimateLogger][" + getTimeStamp() + "]: ";
   }

   private String getWarningHeader() {
      return "[WARN][UltimateLogger][" + getTimeStamp() + "]: ";
   }

   public boolean isVisible() {
      return loggerFrame.isVisible();
   }

   public static String getTimeStamp() {
      Timestamp timestamp = new Timestamp(System.currentTimeMillis());
      return SDF.format(timestamp);
   }
   
   private final static SimpleDateFormat SDF = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");

   private JFrame loggerFrame;
   private JScrollPane scrollPane;
   private final DefaultListModel<String> listModel = new DefaultListModel<String>();
   private JList<String> list;

   private static class CellRender extends DefaultListCellRenderer {
      @Override
      public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
         super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
         if (value.toString().contains("ERROR")) {
            setForeground(Color.RED);
         } else if (value.toString().contains("WARN")) {
            setForeground(Color.ORANGE);
         } else {
            setForeground(Color.GREEN);
         }
         return this;
      }
   }
}
public enum LoggerSeverity {
   
   OKAY, WARN, ERROR;

}
  • Like 2
Link to comment
Share on other sites

On 6/10/2023 at 9:08 AM, Malcolm said:

here is a custom one I made ages ago. Feel free to edit it.

 

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;

import javax.swing.DefaultListCellRenderer;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;

public class UltimateLogger {

   public UltimateLogger() {
      createLogger();
   }

   public void close() {
      loggerFrame.setVisible(false);
      loggerFrame.dispose();
   }

   public void open() {
      loggerFrame.setVisible(true);
   }

   private void createLogger() {
      JTabbedPane loggerTab = getTabbedPane();
      loggerFrame = getFrame();
      loggerFrame.add(loggerTab);
      open();
   }

   private JFrame getFrame() {
      JFrame frame = new JFrame("Ultimate Logger");
      final Dimension frameDimention = new Dimension(650, 350);
      frame.setSize(frameDimention);
      frame.setMinimumSize(frameDimention);
      return frame;
   }

   private JTabbedPane getTabbedPane() {
      JPanel scrollPanel = new JPanel();
      scrollPane = new JScrollPane(scrollPanel);
      list = new JList<String>(listModel);
      scrollPanel.add(list);
      scrollPanel.setLayout(new GridLayout(1, 0));
      JTabbedPane tab = new JTabbedPane();
      tab.addTab("Ultimate Bot Debug", scrollPane);
      return tab;
   }

   public void log(String loggedString) {
      listModel.addElement(getOkHeader() + loggedString);
      list.setCellRenderer(new CellRender());
      setMaximumScroll();
   }

   public void logError(String loggedString) {
      listModel.addElement(getErrorHeader() + loggedString + "\n");
      list.setCellRenderer(new CellRender());
      setMaximumScroll();
   }

   public void logWarning(String loggedString) {
      listModel.addElement(getWarningHeader() + loggedString + "\n");
      list.setCellRenderer(new CellRender());
      setMaximumScroll();
   }

   private void setMaximumScroll() {
      JScrollBar vertical = scrollPane.getVerticalScrollBar();
      vertical.setValue(vertical.getMaximum());
   }

   private String getOkHeader() {
      return "[OKAY][UltimateLogger][" + getTimeStamp() + "]: ";
   }

   private String getErrorHeader() {
      return "[ERROR][UltimateLogger][" + getTimeStamp() + "]: ";
   }

   private String getWarningHeader() {
      return "[WARN][UltimateLogger][" + getTimeStamp() + "]: ";
   }

   public boolean isVisible() {
      return loggerFrame.isVisible();
   }

   public static String getTimeStamp() {
      Timestamp timestamp = new Timestamp(System.currentTimeMillis());
      return SDF.format(timestamp);
   }
   
   private final static SimpleDateFormat SDF = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");

   private JFrame loggerFrame;
   private JScrollPane scrollPane;
   private final DefaultListModel<String> listModel = new DefaultListModel<String>();
   private JList<String> list;

   private static class CellRender extends DefaultListCellRenderer {
      @Override
      public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
         super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
         if (value.toString().contains("ERROR")) {
            setForeground(Color.RED);
         } else if (value.toString().contains("WARN")) {
            setForeground(Color.ORANGE);
         } else {
            setForeground(Color.GREEN);
         }
         return this;
      }
   }
}
public enum LoggerSeverity {
   
   OKAY, WARN, ERROR;

}

Damn thank you thats a lot❤️

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...