<< May 28, 2020 | Home | May 30, 2020 >>

Tomcat Logging

I guess this is something pretty obvious for java users (except me). I have to admit that in development I did tend to use a System.out.println(..) logging. And everytime at a late time when it comes to deployment you realize (again) that syso-logging is not optimal. Switching to that late time to log4j and such is tedious work.

Whatever, now that I make the wood-games-server fit for deployment again, I am at this point again. Actually 9 years ago I seemed to have used an ugly hack, discarding the System.out- and System.err-Stream like this:

Don't:

System.setOut(new PrintStream(new OutputStream() {
	
	@Override
	public void write(int arg0) throws IOException {
		
		
	}
}));

System.setErr(new PrintStream(new OutputStream() {
	
	@Override
	public void write(int arg0) throws IOException {
		// TODO Auto-generated method stub
		
	}
}));

Ok,...now I will do things right. And when using the servlet-based-server rest-server in tomcat (and I guess i any other 'application'-server as well) you can just grab the current active logger. (I'm actually not sure if that is the right way since I still think this goes into catalina.out-log which is shared by every servlet-application!? (Really not sure,...) Nonetheless for my special case I'm going to create a docker image with just one application, so this should be fine for me...(If you want the logs to (also) go into a separate file, you would have to need to add an additional logger-setup in your logging.properties-file)

TL;DR;

Get the current logger:

  import java.util.logging.Logger;
  ...
  public class MyClass{
   ...
   private Logger logger = Logger.getLogger(DatabaseLayer.class.toString());
   ...
  }

Some video about tomcat-logging(speechless but good)

Tags : ,

Hibernate: override data from persistence.xml

If you want to override data you defined in your java-application's jpa/hibernate persistence.xml file you can pass a Map<String,Object> to the persistence-api when creating the entityManagerFactory (in this case using an environment-variable to set the connection url)

Map<String, Object> configOverrides = new HashMap<String, Object>();
configOverrides.put("hibernate.hbm2ddl.auto", "create-drop");

// override: <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/mydb?autoReconnect=true"/>
String overrideDbHost = System.getenv("FAW_DB_HOST_STRING");
if (overrideDbHost!=null) {
  System.out.println("Override DB_HOST to "+overrideDbHost);
  configOverrides.put("hibernate.connection.url", "jdbc:mysql://"+overrideDbHost+"/mydb?autoReconnect=true");
}
emf = Persistence.createEntityManagerFactory("mydbconf", configOverrides);

Read more:

<< May 28, 2020 | Home | May 30, 2020 >>