Can I use slf4j in KNIME ?

I want to use slf4j API loggers in my code but the log messages are not written in the knime.log file. I knwo that KNIME uses log4j… How can I bind slf4j with log4j in order to see my messages?

Hi @goulpan,

Is there are very specific reason you need to use raw slf4j?
I’d recommend you to try the NodeLogger, which will log into the KNIME log.
To log to the KNIME log you need to use the NodeLogger class (knime-core/NodeLogger.java at master · knime/knime-core · GitHub).
You create your own instance by calling NodeLogger.getLogger(YourJava.class).

best,
Gabriel

I will be using the same code in two separate places, one of them not being KNIME platform. So I would like it not to depend on any KNIME libraries.
I am aware of NodeLogger, but that would mean including KNIME jars in a part of my code which is used also in a web app, which has nothing to do with KNIME.

Hi @goulpan,

This is also possible, we have a slf4j binding available, so in theory you can just call:

final Logger logger = LoggerFactory.getLogger(getClass());

However, this log is filtered, to prevent the log being flooded by third party log messages. To whitelist your log messages, you will need to create a NodeLogger instance with the shared package prefix.
Take a look at the following example:


Example

Given two projects, Library and KNIME Extension, with the latter making use of the former.

Library:

package com.example.shared;

class ExampleClass {
   final Logger logger = LoggerFactory.getLogger(getClass());
}

KNIME extension

package com.example.knime;

class KNIMEExampleClass {
   final NodeLogger logger = NodeLogger.getLogger("com.example.shared");
}

best,
Gabriel

You can easily create a custom binding which will redirect from SLF4J in your lib./custom code to the KNIME logger (that’s in fact the advantage of SLF4J). Afair you’d simply need to subclass some classes from the SLF4J API. We do that in one of our plugins. If you need further input, I can probably help you out with some sample code. Feel free to get back!

–Philipp

2 Likes

Thank you all for your prompt replies… I just realised that I am using an earlier version of KNIME (v4.2.2) which does not include slf4j bindings. I just upgraded to v4.3.x and that solved my problem.

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.