4

By default, the JSS Node.js rendering engine captures console output / errors emitted by the Node instance(s) rendering your JSS app and logs the output / errors to the default Sitecore log.

I would like to replace the built-in JSS/Sitecore logger with a custom logger, how can I go about doing so?

4

To implement a custom logger, you'll need to implement a type that extends the Sitecore.JavaScriptServices.ViewEngine.NodeServices.ILogger interface from the Sitecore.JavaScriptServices.ViewEngine.NodeServices assembly.

(For reference, use your favorite disassembler to inspect the default JSS/Sitecore implementation that logs Node.js output to Sitecore logs in the Sitecore.JavaScriptServices.ViewEngine.Node.Logger class in the Sitecore.JavaScriptServices.ViewEngine.Node assembly.)

You'll then need to register your ILogger implementation with the Sitecore DI container with something like this (where MyCustomLogger is your custom implementation of ILogger:

using Microsoft.Extensions.DependencyInjection;
using Sitecore.DependencyInjection;
using Sitecore.JavaScriptServices.ViewEngine.NodeServices;

namespace MyNamespace
{
    public class RegisterDependenciesPatch : IServicesConfigurator
    {
        public void Configure(IServiceCollection serviceCollection)
        {
            var toRemove = serviceCollection.FirstOrDefault(d => d.ServiceType == typeof(ILogger));
            serviceCollection.Remove(toRemove);
            serviceCollection.AddTransiet<ILogger, MyCustomLogger>();
        }
    }
}

Lastly, you'll need to patch the App_Config/Sitecore/JavaScriptServices/Sitecore.JavaScriptServices.ViewEngine.Node.config file to add your dependency registration code after the default JSS Node services dependency registrations. Example:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <configurator
      type="MyNamespace.RegisterDependenciesPatch, MyLibrary"
      patch:after="*[@type='Sitecore.JavaScriptServices.ViewEngine.Node.RegisterDependencies, Sitecore.JavaScriptServices.ViewEngine.Node']"
    />
  </sitecore>
</configuration>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.