Reputation: 12039
Is there a way to use a Seam Logger inside of a standard Java Servlet? I have one servlet that I use to serve up content, while the rest of my application is developed using Seam. I'd like to be able to make use of Seam's built-in Log functionality in my servlet, the same way I do with the rest of my Seam-based classes. In my Servlet constructor, I tried calling
Log logger = (Log)Component.getInstance(Log.class);
...but I'm getting an exception. Is there a better way to "inject" a Logger into the servlet? Something I'm missing? Thanks!!
Upvotes: 4
Views: 1757
Reputation: 68942
To use a Seam context you can wrap the doPost
or doGet
methods with ContextualHttpServletRequest
which allows you to do Component lookups.
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
final HttpServletRequest req = request;
new ContextualHttpServletRequest( req ) {
public void process() throws Exception {
wrappedPost( req );
}
}.run();
}
// you renamed original doPost method
private void wrappedPost(HttpServletRequest request) {
...
}
The above example code allows you to access components by name, which are declared using the name annotation:
@Name("MyCcomponent")
The Logger is usually injected by the @Logger
annotation, I checked logs the source package, there is no component with a @Name
annotation that could be looked up. If you call a component (with @Logger) from the wrapped context, a logger will be injected (into the called compontent).
Seam uses in its components:
private static final LogProvider log = Logging
.getLogProvider(MailSession.class);
Upvotes: 3
Reputation: 5314
I didn't tested it, but you could try this :
Log mylog = Logging.getLogger(MyClass.class);
That's what i use in my tests.
Upvotes: 3