From 796a1b75bc7c1097c1a631d6b03c7dbe4d4dd9e7 Mon Sep 17 00:00:00 2001 From: Patrick Huang Date: Thu, 6 Apr 2017 12:38:53 +1000 Subject: [PATCH] ZNTA-1856 - fix logging on jenkins slave --- .../zanata/zanatareposync/ZanataSyncStep.java | 63 ++++++++++++++----- 1 file changed, 48 insertions(+), 15 deletions(-) diff --git a/src/main/java/org/jenkinsci/plugins/zanata/zanatareposync/ZanataSyncStep.java b/src/main/java/org/jenkinsci/plugins/zanata/zanatareposync/ZanataSyncStep.java index 989011d..e7baf60 100644 --- a/src/main/java/org/jenkinsci/plugins/zanata/zanatareposync/ZanataSyncStep.java +++ b/src/main/java/org/jenkinsci/plugins/zanata/zanatareposync/ZanataSyncStep.java @@ -1,3 +1,23 @@ +/* + * Copyright 2016, Red Hat, Inc. and individual contributors + * as indicated by the @author tags. See the copyright.txt file in the + * distribution for a full listing of individual contributors. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ package org.jenkinsci.plugins.zanata.zanatareposync; import java.io.File; @@ -54,9 +74,11 @@ import jenkins.tasks.SimpleBuildStep; /** - * Zanata builder that uses the bundled zanata client commands classes. + * A build step that uses the bundled zanata client commands classes to perform + * zanata push and pull. * - * @author Patrick Huang pahuang@redhat.com + * @author Patrick Huang + * pahuang@redhat.com */ public class ZanataSyncStep extends Builder implements SimpleBuildStep, HasSyncJobDetail { @@ -165,7 +187,6 @@ public void setPullFromZanata(boolean pullFromZanata) { public void perform(Run build, FilePath workspace, Launcher launcher, TaskListener listener) throws IOException { // This is where you 'build' the project. - Handler logHandler = configLogger(listener.getLogger()); StandardUsernameCredentials cred = CredentialsProvider.findCredentialById(zanataCredentialsId, StandardUsernameCredentials.class, build); if (cred == null) { @@ -187,20 +208,18 @@ public void perform(Run build, FilePath workspace, Launcher launcher, Task try { if (pushToZanata) { - pushToZanata(workspace, zanataSyncService); + pushToZanata(workspace, zanataSyncService, listener); } if (pullFromZanata) { Git git = Git.with(listener, new EnvVars(EnvVars.masterEnvVars)); GitSyncService gitSyncService = new GitSyncService(this, git); - pullFromZanata(workspace, zanataSyncService, gitSyncService); + pullFromZanata(workspace, zanataSyncService, gitSyncService, listener); } } catch (IOException | InterruptedException e) { logger(listener).println("Zanata Sync failed:" + e.getMessage()); throw new RuntimeException(e); - } finally { - removeLogger(logHandler); } } @@ -220,20 +239,29 @@ private static Handler configLogger(PrintStream printStream) { return loggerHandler; } - private static void removeLogger(Handler appender) { - java.util.logging.Logger.getLogger("org.zanata").removeHandler(appender); + private static void removeLogger(Handler handler) { + if (handler != null) { + java.util.logging.Logger.getLogger("org.zanata").removeHandler(handler); + } } private static void pullFromZanata(FilePath workspace, - final ZanataSyncServiceImpl service, GitSyncService gitSyncService) + final ZanataSyncServiceImpl service, GitSyncService gitSyncService, + TaskListener listener) throws IOException, InterruptedException { workspace.act(new FilePath.FileCallable() { @Override public Void invoke(File f, VirtualChannel channel) throws IOException, InterruptedException { - service.pullFromZanata(f.toPath()); - gitSyncService.syncTranslationToRepo(f.toPath()); + Handler handler = null; + try { + handler = configLogger(listener.getLogger()); + service.pullFromZanata(f.toPath()); + gitSyncService.syncTranslationToRepo(f.toPath()); + } finally { + removeLogger(handler); + } return null; } @@ -245,14 +273,19 @@ public void checkRoles(RoleChecker roleChecker) } private static void pushToZanata(FilePath workspace, - final ZanataSyncServiceImpl service) + final ZanataSyncServiceImpl service, TaskListener listener) throws IOException, InterruptedException { workspace.act(new FilePath.FileCallable() { @Override public Void invoke(File f, VirtualChannel channel) throws IOException, InterruptedException { - - service.pushToZanata(f.toPath()); + Handler handler = null; + try { + handler = configLogger(listener.getLogger()); + service.pushToZanata(f.toPath()); + } finally { + removeLogger(handler); + } return null; }