public static LoggerContext buildLoggerContext(Map<String, String> props) { if (loggerContext == null) { ILoggerFactory lcObject = LoggerFactory.getILoggerFactory(); if (!(lcObject instanceof LoggerContext)) { throw new LogbackException( "Expected LOGBACK binding with SLF4J, but another log system has taken the place: " + lcObject.getClass().getSimpleName()); } loggerContext = (LoggerContext) lcObject; if (props != null) { for (Map.Entry<String, String> entry : props.entrySet()) { loggerContext.putProperty(entry.getKey(), entry.getValue()); } } } return loggerContext; }
/** configure logging */ private void logging(CliRequest cliRequest) { cliRequest.debug = cliRequest.commandLine.hasOption(CLIManager.DEBUG); cliRequest.quiet = !cliRequest.debug && cliRequest.commandLine.hasOption(CLIManager.QUIET); cliRequest.showErrors = cliRequest.debug || cliRequest.commandLine.hasOption(CLIManager.ERRORS); slf4jLoggerFactory = LoggerFactory.getILoggerFactory(); Slf4jConfiguration slf4jConfiguration = Slf4jConfigurationFactory.getConfiguration(slf4jLoggerFactory); if (cliRequest.debug) { cliRequest.request.setLoggingLevel(MavenExecutionRequest.LOGGING_LEVEL_DEBUG); slf4jConfiguration.setRootLoggerLevel(Slf4jConfiguration.Level.DEBUG); } else if (cliRequest.quiet) { cliRequest.request.setLoggingLevel(MavenExecutionRequest.LOGGING_LEVEL_ERROR); slf4jConfiguration.setRootLoggerLevel(Slf4jConfiguration.Level.ERROR); } // else fall back to default log level specified in conf // see http://jira.codehaus.org/browse/MNG-2570 if (cliRequest.commandLine.hasOption(CLIManager.LOG_FILE)) { File logFile = new File(cliRequest.commandLine.getOptionValue(CLIManager.LOG_FILE)); logFile = resolveFile(logFile, cliRequest.workingDirectory); // redirect stdout and stderr to file try { PrintStream ps = new PrintStream(new FileOutputStream(logFile)); System.setOut(ps); System.setErr(ps); } catch (FileNotFoundException e) { // // Ignore // } } slf4jConfiguration.activate(); plexusLoggerManager = new Slf4jLoggerManager(); slf4jLogger = slf4jLoggerFactory.getLogger(this.getClass().getName()); }
private PlexusContainer container(CliRequest cliRequest) throws Exception { if (cliRequest.classWorld == null) { cliRequest.classWorld = new ClassWorld("plexus.core", Thread.currentThread().getContextClassLoader()); } DefaultPlexusContainer container; ContainerConfiguration cc = new DefaultContainerConfiguration() .setClassWorld(cliRequest.classWorld) .setRealm(setupContainerRealm(cliRequest)) .setClassPathScanning(PlexusConstants.SCANNING_INDEX) .setAutoWiring(true) .setName("maven"); container = new DefaultPlexusContainer( cc, new AbstractModule() { protected void configure() { bind(ILoggerFactory.class).toInstance(slf4jLoggerFactory); } }); // NOTE: To avoid inconsistencies, we'll use the TCCL exclusively for lookups container.setLookupRealm(null); container.setLoggerManager(plexusLoggerManager); customizeContainer(container); container.getLoggerManager().setThresholds(cliRequest.request.getLoggingLevel()); Thread.currentThread().setContextClassLoader(container.getContainerRealm()); eventSpyDispatcher = container.lookup(EventSpyDispatcher.class); DefaultEventSpyContext eventSpyContext = new DefaultEventSpyContext(); Map<String, Object> data = eventSpyContext.getData(); data.put("plexus", container); data.put("workingDirectory", cliRequest.workingDirectory); data.put("systemProperties", cliRequest.systemProperties); data.put("userProperties", cliRequest.userProperties); data.put("versionProperties", CLIReportingUtils.getBuildProperties()); eventSpyDispatcher.init(eventSpyContext); // refresh logger in case container got customized by spy slf4jLogger = slf4jLoggerFactory.getLogger(this.getClass().getName()); maven = container.lookup(Maven.class); executionRequestPopulator = container.lookup(MavenExecutionRequestPopulator.class); modelProcessor = createModelProcessor(container); settingsBuilder = container.lookup(SettingsBuilder.class); toolchainsBuilder = container.lookup(ToolchainsBuilder.class); dispatcher = (DefaultSecDispatcher) container.lookup(SecDispatcher.class, "maven"); return container; }
@Nonnull protected Logger logger(@Nonnull String name) { return _loggerFactory != null ? _loggerFactory.getLogger(name) : LoggerFactory.getLogger(name); }