Skip to content

Commit

Permalink
Add Java System logger to the jsch instance
Browse files Browse the repository at this point in the history
Shift log level to less serious level
 - Jsch issues a lot of INFO messages which are rather DEBUG level and warnings, which are rather info level
Signed-off-by:Ondro Mihalyi <mihalyi@omnifish.ee>
  • Loading branch information
OndroMih committed Jun 10, 2023
1 parent fbde2ec commit 80bf99f
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 2 deletions.
Expand Up @@ -139,6 +139,7 @@ jakarta.enterprise.system.core.naming.level=INFO
jakarta.enterprise.system.core.security.level=INFO
jakarta.enterprise.system.core.security.web.level=INFO
jakarta.enterprise.system.core.selfmanagement.level=INFO
jakarta.enterprise.system.core.ssh.level=INFO
jakarta.enterprise.system.core.transaction.level=INFO
jakarta.enterprise.system.jmx.level=INFO
jakarta.enterprise.system.security.ssl.level=INFO
Expand Down
@@ -0,0 +1,70 @@
/*
* Copyright (c) 2023 Contributors to the Eclipse Foundation
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package org.glassfish.cluster.ssh.launcher;

import com.jcraft.jsch.JSch;
import java.lang.System.Logger;
import java.lang.System.Logger.Level;

public class JavaSystemJschLogger implements com.jcraft.jsch.Logger {

private final Logger logger;

public JavaSystemJschLogger() {
this.logger = System.getLogger(JSch.class.getName());
}

public JavaSystemJschLogger(String loggerName) {
this.logger = System.getLogger(loggerName);
}

@Override
public boolean isEnabled(int jschLevel) {
return logger.isLoggable(getSystemLoggerLevel(jschLevel));
}

@Override
public void log(int jschLevel, String message) {
logger.log(getSystemLoggerLevel(jschLevel), message);
}

@Override
public void log(int jschLevel, String message, Throwable cause) {
if (cause != null) {
logger.log(getSystemLoggerLevel(jschLevel), message, cause);
} else {
logger.log(getSystemLoggerLevel(jschLevel), message);
}
}

static Level getSystemLoggerLevel(int jschLevel) {
switch (jschLevel) {
case DEBUG:
return Level.TRACE;
case INFO:
return Level.DEBUG;
case WARN:
return Level.INFO;
case ERROR:
return Level.WARNING;
case FATAL:
return Level.ERROR;
default:
return Level.TRACE;
}
}

}
@@ -1,4 +1,5 @@
/*
* Copyright (c) 2023 Contributors to the Eclipse Foundation
* Copyright (c) 1997, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand Down Expand Up @@ -191,8 +192,7 @@ public void init(String userName, String host, int port, String password, String
private void openConnection() throws JSchException {
assert session == null;
JSch jsch = new JSch();
// TODO: Logger?
// jsch.setLogger(logger);
jsch.setLogger(new JavaSystemJschLogger(logger != null ? logger.getName() + ".ssh" : JSch.class.getName()));

// Client Auth
String message = "";
Expand Down

0 comments on commit 80bf99f

Please sign in to comment.