Adding Logging to a pre-existing Program

I have a game that I have been writing in java as a hobby for a number of years. I have very sparse logging, just some simple output when something goes really wrong. It’s about 25,000 lines of code.

I just released a first beta, and I’m realizing it is going to be a nightmare to deal with bug reports without good logs. Whenever something goes wrong on my test machine, I have the luxury of looking at the code and adding print statements if I need to figure out what’s going wrong. Obviously, that’s not available once its released in the wild.

  1. Do you think it is advisable to try to add logging to this program now? Or do you think that I’m too far along to try to fix this problem?
  2. Do you have any recommendations on how to go about doing it? I honestly don’t know where to start.
  3. Do you have a recommendation for a logging system for Java?
  4. Do you have any good articles / best practices recommendations on what to log and how to log it?

Thanks! Any and all information is greatly appreciated.

The rule of thumb when it comes to logging: “it’s never too late!”

Do you think it is advisable to try to add logging to this program
now? Or do you think that I’m too far along to try to fix this
problem?

You can start off with some generic logging, which is agnostic to your application as such:

  • ENTRY/EXIT logs: You can add a logging statement at the start of every method and at every exit point at the TRACE level. (Logging the in/out arguments is optional)

    public int myMethod(int input) {
        logger.trace(String.format("+++ [%d]", input));
        .
        .
        .
        logger.trace(String.format("--- [%d]", output));
        return output;
    }
    

This may sound trivial and benign at first, but if you compare having zero logs v/a having a trail of TRACE logs, you’d be far better with the latter. It also helps in encapsulating logs from a single method between the ENTRY and EXIT statements in the logfile.

It can be cumbersome to add such logs in every method ever, so you can choose to be a ninja and use Aspect-Oriented Programming to do it quicker: How to automatically log the entry/exit of methods in Java?

  • EXCEPTIONS: You can quickly replace all your e.printStackTrace() statements with a logging statement that puts the stack trace in the log file.

Do you have any recommendations on how to go about doing it? I
honestly don’t know where to start.

Assuming you don’t want to become an expert in logging and are simply looking for a way to ‘logify’ your code before the beta release, you can take a look at this quick tutorial on adding log statements.

Do you have a recommendation for a logging system for Java?

If you want to start quick, use log4j with the PatternLayout. All it takes is a simple .properties file and the JAR included to your classpath to see the logs rolling.

If you want to evaluate several logging libraries, but don’t have the time right now, then I would suggest starting off with log4j but use the slf4j wrapper on top of it. What slf4j does is, it abstracts out the logging library so that later, you can easily swap log4j for something else, like logback, without having to change imports and logging statement across your code.

Do you have any good articles / best practices recommendations on what
to log and how to log it?

A few suggestions off the top of my head:

  • Declare a static logger object for every class: this will ensure proper location reporting of where the log statements come from

    final static Logger logger = Logger.getLogger(classname.class);
    
  • Log an INFO message for every successful operation that a method has done. It helps to not have a bird’s-eye view at the code and only focus on one method at once instead when adding logs for functionality (and is also quicker since you can warp through your code base adding logs like a machine!)

  • Make sure you log the EXCEPTION in the ERROR message in your catch() block – this will log the stack trace in the logfile instead of just the message
  • Log a TRACE or an INFO just before starting time-intensive operations, so that if a pedantic user is staring at the logs while your program is running, he/she will be tranquilized by the fact that something time-intensive is happening and the program hasn’t crashed just because the logging paused for a while

Happy logging! Be assured that your efforts will pay off.

I’d suggest reading up on Aspect-Oriented Programming, specifically as it applies to logging:
http://www.yegor256.com/2014/06/01/aop-aspectj-java-method-logging.html

AOP is a technique for keeping your methods clean and focused on business logic while extracting “plumbing” code – such as security, validation or, yes, logging – to external classes, and having the infrastructure apply them automatically. In this case, you create an Aspect called “MethodLogger” and apply it to your classes, and it hooks into your code before and/or after each method execution and performs its code, which can log the method call and its parameters.

It’s not perfect logging, especially if you have long and complicated methods that need logging their computation state in the middle, but it can give you a whole lot of logging info, throughout your program, in one go.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật