How to log in an OSGi plugin

22 views
Skip to first unread message

Xander Steinmann

unread,
Apr 22, 2021, 5:20:23 AM4/22/21
to dotCMS User Group
What is the best way to log in an OSGi plugin? System.out does not work (and is bad practice) and the com.dotmarketing.util.Logger does not work out of the box.

In the past we added this piece of code to a plugin in order to enable logging through the Logger (you need to export some logging classes for this to work):

LoggerContext dotcmsLoggerContext = Log4jUtil.getLoggerContext();
if (dotcmsLoggerContext != null) {
  //Initialing the log4j context of this plugin based on the dotCMS logger context
  pluginLoggerContext = (LoggerContext)   LogManager.getContext(this.getClass().getClassLoader(), false, dotcmsLoggerContext, dotcmsLoggerContext.getConfigLocation());
}

But since dotCMS 5.3.8 this also doesn't work any more. Any ideas on how to enable logging to the dotcms.log in an OSGi plugin?

Kind regards,

Xander

Falzone, Chris

unread,
Apr 22, 2021, 7:52:47 AM4/22/21
to dot...@googlegroups.com
That's what I have in mine and it seems to work at least in 5.0.3 and 20.11.1.  Maybe something broke and they fixed it in later versions?  They still have this in the docs:

https://dotcms.com/docs/latest/osgi-dynamic-plugin-logging

Though I see the package names are not correct.  They don't have the repackaged stuff anymore, so that needs to be updated.

--
http://dotcms.com - Open Source Java Content Management
---
You received this message because you are subscribed to the Google Groups "dotCMS User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dotcms+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/dotcms/a552d242-a253-4eed-9ad1-01d89784e006n%40googlegroups.com.


--

Christopher Falzone

DevOps Engineer

A Q U E N T  /  VITAMIN T

jonathan...@dotcms.com

unread,
Apr 22, 2021, 11:49:50 AM4/22/21
to dotCMS User Group
Hi Xander,

Depending on the version, you can do something like

Logger.info(this.getClass().getName(), "Some log");

Always use the class as a String, instead of a class since the object could be not accesible (does not exists) on dotCMS.

Best,
J

Xander Steinmann

unread,
May 4, 2021, 10:00:46 AM5/4/21
to dotCMS User Group
We've updated the dotCMS Logger according to this fix:  #20039
This helped in our case.

Kind regards,

Xander
Op donderdag 22 april 2021 om 17:49:50 UTC+2 schreef jonathan...@dotcms.com:

Nathan Keiter

unread,
May 4, 2021, 10:50:43 AM5/4/21
to dot...@googlegroups.com
Yeah somewhere along the 5.x line using the class no longer works for the logger. You have to use the text string now.


I don't know what changed... maybe a version change on the OSGI framework?

Nathan I. Keiter | Lead Network Applications Programmer | I.D.E.A Council Member
Gettysburg College | Information Technology | DataSystems
Campus Box 2453 | 300 North Washington Street | Gettysburg, PA 17325
Phone: 717.337.6993
https://www.gettysburg.edu<https://www.gettysburg.edu/>
________________________________
From: dot...@googlegroups.com <dot...@googlegroups.com> on behalf of Xander Steinmann <xan...@isaac.nl>
Sent: Tuesday, May 4, 2021 10:00 AM
To: dotCMS User Group
Subject: [dotcms] Re: How to log in an OSGi plugin

CAUTION: This email originated from outside of the organization. Do not click links or open attachments unless you recognize the sender and know the content is safe.
________________________________
We've updated the dotCMS Logger according to this fix: #20039<https://linkprotect.cudasvc.com/url?a=https%3a%2f%2fgo.dotcms.com%2fe2t%2ftc%2fMWNWQ7SgnR9W4b4BbP84mZxVW7FnbcL4r7mvqN1vWRrZ3lGmQV1-WJV7CgHFtW8jR0ws1jWXm4W1_kmXb3WXvyJW2T4dbQ4kpt7PW4MbRWr4L9s4qW7Yy-lN2D8Y8vW1S5Hll1sc0C4W5q0ZyY7P6z2gW35yPx32Wvy6DW5JSf4d6JBQXTW6jLSVV1_ZYlsW5_8XGb2ZRSb2W5pTlnL6hhsbXVwfvHt2N2g-jW6bz4qq1G4dv1W8rf65f84SqT6W53r-sf8xNbQFW8Zs2cX1YBXQKW23NVzn6gltQmW2Ch-PY2tHYC1N3b8w-NxgvhLW8btnl52zZXBNW86zH0M21FGTX3ch21&c=E,1,vS6Q9OyBcoZzTQrr11ODHWtnHyCxS6AicPncseziyUWSK5oPUQDm3JexkVDeCE3sdm7d3g8Bz1IK6h9_gKmkp2a2ovP8zy6a-W4ISN8pEttCJmw,&typo=1>
--
http://dotcms.com<https://linkprotect.cudasvc.com/url?a=http%3a%2f%2fdotcms.com&c=E,1,83lcokXbRymOG71_VG1o1joHrT_O9RmlUSqBFlmpXBRIr4kCLXxZcGRQeUYbDBp7oT97zCiH26tToVsNwjpp1RoB6Vdi4_UkJU7NXg_GOwl9cg7udJ-y&typo=1> - Open Source Java Content Management
---
You received this message because you are subscribed to the Google Groups "dotCMS User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dotcms+un...@googlegroups.com<mailto:dotcms+un...@googlegroups.com>.
To view this discussion on the web visit https://groups.google.com/d/msgid/dotcms/92586377-8cc6-4f06-b73b-751417e3a511n%40googlegroups.com<https://linkprotect.cudasvc.com/url?a=https%3a%2f%2fgroups.google.com%2fd%2fmsgid%2fdotcms%2f92586377-8cc6-4f06-b73b-751417e3a511n%2540googlegroups.com%3futm_medium%3demail%26utm_source%3dfooter&c=E,1,qlBx81a6bOhoqw5YjRWRj0vW4kB-6q7gQz3JnWcYH7xwVrP6Ib0PGam3ImCGN2c9Q5uK78DbqQhoRlim9gve6StfoSgJGl9J-E_NALi7-0c,&typo=1>.

Nathan Keiter

unread,
May 18, 2021, 11:19:03 AM5/18/21
to dot...@googlegroups.com
Thanks Xander! I was also able to patch our version (dotCMS 5.2.1) based on the fix to the bug you shared here https://github.com/dotCMS/core/issues/20039


Now we can log with the class itself again as in 4.x and prior versions!


This works much better, especially when you have custom logs defined in your Log4J config. (All of our plugins write to a separate log file so they don't get lost in dotcms.log.) When using class.getName() the Log4J rules don't work.


The patch contains two other classes not found in our dotCMS version that were required to compile it. Other than that it was pretty easy.

Nathan I. Keiter | Lead Network Applications Programmer | I.D.E.A Council Member
Gettysburg College | Information Technology | DataSystems
Campus Box 2453 | 300 North Washington Street | Gettysburg, PA 17325
Phone: 717.337.6993
https://www.gettysburg.edu<https://www.gettysburg.edu/>
________________________________
From: dot...@googlegroups.com <dot...@googlegroups.com> on behalf of Xander Steinmann <xan...@isaac.nl>
Sent: Tuesday, May 4, 2021 10:00 AM
To: dotCMS User Group
Subject: [dotcms] Re: How to log in an OSGi plugin

CAUTION: This email originated from outside of the organization. Do not click links or open attachments unless you recognize the sender and know the content is safe.
________________________________
We've updated the dotCMS Logger according to this fix: #20039<https://linkprotect.cudasvc.com/url?a=https%3a%2f%2fgo.dotcms.com%2fe2t%2ftc%2fMWNWQ7SgnR9W4b4BbP84mZxVW7FnbcL4r7mvqN1vWRrZ3lGmQV1-WJV7CgHFtW8jR0ws1jWXm4W1_kmXb3WXvyJW2T4dbQ4kpt7PW4MbRWr4L9s4qW7Yy-lN2D8Y8vW1S5Hll1sc0C4W5q0ZyY7P6z2gW35yPx32Wvy6DW5JSf4d6JBQXTW6jLSVV1_ZYlsW5_8XGb2ZRSb2W5pTlnL6hhsbXVwfvHt2N2g-jW6bz4qq1G4dv1W8rf65f84SqT6W53r-sf8xNbQFW8Zs2cX1YBXQKW23NVzn6gltQmW2Ch-PY2tHYC1N3b8w-NxgvhLW8btnl52zZXBNW86zH0M21FGTX3ch21&c=E,1,vS6Q9OyBcoZzTQrr11ODHWtnHyCxS6AicPncseziyUWSK5oPUQDm3JexkVDeCE3sdm7d3g8Bz1IK6h9_gKmkp2a2ovP8zy6a-W4ISN8pEttCJmw,&typo=1>
global-override-logger.zip
Reply all
Reply to author
Forward
0 new messages