javalog4jtomcat8ghost4j

log4j not working on tomcat8 server with log4j.properties file


I am trying to convert some PDFs files into PNGs using Ghost4j. This library needs log4j for working, so I added to my libraries also log4j.

Since my application is a Dynamic Web Project, I added the libraries into the folder WEB-INF/lib.

When I tried to run the application on my local tomcat8 server, it was logging some warnings such as:

log4j:WARN No appenders could be found for logger (org.ghost4j.Ghostscript).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

So, surfing the web, I found that I was missing the log4j.properties file in my application. Thus, I created and added the file log4j.properties into WEB-INF/classes (as suggested on some other posts of stackoverflow). With this fix, then, the application on my locat tomcat8 server was running smoothly.

While, when I tried to deploy it on the remote tomcat8 server, the application does not work. The problem is that it does not produce any Exception of any kind, it just interrupts its work. With or without the log4j.properties file, on the remote tomcat8 server there was no difference: it just stops running at the same "code line" in which previously on my local server was logging that warnings I wrote before.

Any help is apreciated.

P.S.: The code for the conversion is pretty easy. I post it to complete my question:

private void createImages(String machine, String fileName){
        LOGGER.warning("started creating images");
        try{
            PDFDocument document = new PDFDocument();
            document.load(new File(DOCS_DIR + machine + "/" + fileName + ".pdf"));

            String commonPath = IMGS_DIR + machine + "/" + fileName + "/";
            Path path = Paths.get(new File(commonPath).getPath());
            if(Files.notExists(path, LinkOption.NOFOLLOW_LINKS)){
                new File(commonPath).mkdirs();
            }
            SimpleRenderer renderer = new SimpleRenderer();
            renderer.setResolution(300);
            renderer.setAntialiasing(SimpleRenderer.OPTION_ANTIALIASING_HIGH);

            LOGGER.warning("IT STOPS HERE!!");
            List<Image> images = renderer.render(document); // this is the line in which the execution of the program stops..
            LOGGER.warning("pdf pages are: " + images.size());
            for (int i = 0; i < images.size(); i++) {
                Image img = images.get(i);
                Image scaledImg = img.getScaledInstance(1200, -1, Image.SCALE_SMOOTH);
                BufferedImage newImage = new BufferedImage(scaledImg.getWidth(null), scaledImg.getHeight(null), BufferedImage.TYPE_INT_ARGB);
                Graphics2D g = newImage.createGraphics();
                g.drawImage(scaledImg, 0, 0, null);
                g.dispose();
                String extension = ".png";
                String imgName = commonPath + (i + 1) + extension;
                LOGGER.warning("creating img n: " + (i+1) + " - creating img in folder: " + imgName);
                ImageIO.write((RenderedImage) newImage, "png", new File(imgName));
            }

            LOGGER.warning("finished creating images!");
        } catch(FileNotFoundException e){
            LOGGER.warning("ERROR DOCUMENT SERVICE -- FileNotFoundException");
            LOGGER.warning(e.printStackTrace());
        } catch (IOException e) {
            LOGGER.warning("ERROR DOCUMENT SERVICE -- IOException");
            LOGGER.warning(e.printStackTrace());
        } catch (RendererException e) {
            LOGGER.warning("ERROR DOCUMENT SERVICE -- RendererException");
            LOGGER.warning(e.printStackTrace());
        } catch (DocumentException e) {
            LOGGER.warning("ERROR DOCUMENT SERVICE -- DocumentException");
            LOGGER.warning(e.printStackTrace());
        } catch (Exception e){
            LOGGER.warning("ERROR DOCUMENT SERVICE -- Exception");
            LOGGER.warning(e.printStackTrace());
        }
    }

Solution

  • At the end I found the solution myself.

    The problem was not related to log4j in any way, but it was not working because it was missing Ghostscript on the server.

    After installing Ghostscript on the server, everything was working fine.