Google Analytics

2015年10月20日 星期二

使用log4jdbc輸出SQL語法

簡介

若是在專案開發與偵錯的過程中,難免都會遇到希望列印完整的SQL Command(及prepare statement的value),這類的solution筆者用過的有P6SPY(https://github.com/p6spy/p6spy)與log4jdbc(https://code.google.com/p/log4jdbc/),本文介紹其中的log4jdbc。

使用方式(以1.2版為例)

Step 1. 先確認使用的JDK搭配的JDBC版本
  • 若為JDK 1.4 or 1.5,請使用搭配的JDBC 3的log4jdbc3-1.2.jar
  • 若為JDK 1.6以上版本,請使用搭配的JDBC 4的log4jdbc4-1.2.jar
請注意,不是用jdbc driver本身是type 3 or 4來判斷的!

Step 2. 選擇輸出日誌的框架
log4jdbc是使用SLF4J的Simple Logging Facade來輸出log的,但如同SLF4J,也可以支援log4j, java.util.logging, Apache Commons Logging, Logback。以筆者用的log4j為例,使用時要在classpath內增加以下jar擋:
  • slf4j-api-1.6.0.jar
  • slf4j-log4j12-1.6.0.jar
Step 3. 將jdbc driver的class name改為net.sf.log4jdbc.DriverSpy

如:
jdbc.driverClass=net.sourceforge.jtds.jdbc.Driver

改寫為:
jdbc.driverClass=net.sf.log4jdbc.DriverSpy

Step 4. 改寫jdbc url,將jdbc:XXXX改為jdbc:log4jdbc:XXX

如:
jdbc.jdbcUrl=jdbc:jtds:sqlserver://localhost:1433/app4use;instance=SQLEXPRESS

改寫為:
jdbc.jdbcUrl=jdbc:log4jdbc:jtds:sqlserver://localhost:1433/app4use;instance=SQLEXPRESS

Step 5. 修改log設定檔案,顯示log4jdbc的訊息

log4jdbc共有以下5個logger可供設定,每一個logger皆可以設定ERROR, INFO, DEBUG層級(FATAL表示關閉):
  • jdbc.sqlonly : 顯示執行的SQL Command
  • jdbc.sqltiming : 顯示執行的SQL Command及其耗費時間(若開啟此logger,則jdbc.sqlonly可以不用開)
  • jdbc.audit : 顯示除了resultset之外的所有JDBC Calls,會產生大量的log內容
  • jdbc.resultset : 顯示resultset的內容,會產生更大量的log內容
  • jdbc.connection : 顯示DB Connection的open, close, commit等事件發生的log,在追查connection pool調度問題時相當有用
log4j.properties範例如下:
log4j.rootLogger=INFO, stdout

log4j.logger.jdbc.sqlonly=FATAL, stdout
log4j.logger.jdbc.sqltiming=INFO, stdout
log4j.logger.jdbc.audit=FATAL, stdout
log4j.logger.jdbc.resultset=FATAL, stdout
log4j.logger.jdbc.connection=INFO, stdout

log4j.additivity.jdbc.sqlonly=false
log4j.additivity.jdbc.sqltiming=false
log4j.additivity.jdbc.audit=false
log4j.additivity.jdbc.resultset=false
log4j.additivity.jdbc.connection=false

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p [%t] (%c:%L) - %m%n


執行結果範例

 15:48:12,939  INFO [schedulerFactoryBean_Worker-3] (jdbc.connection:537) - 41. Connection opened
15:48:12,939  INFO [schedulerFactoryBean_Worker-3] (com.tstartel.estore.service.impl.MailMessageRecServiceImpl:126) - doImportJobErrorRec() - start
15:48:12,963  INFO [schedulerFactoryBean_Worker-3] (jdbc.sqltiming:370) - insert into mail_message_rec (mail_from, mail_to, mail_cc_to, subject, mail_type, send_type,
status, expected_sending_time, actual_sending_time, send_numbers, is_attachment, message_content_path,
message_content, create_time) values ('webmaster@company.com.tw', 'user@company.com.tw', '出貨接收回覆作業失敗', NULL, NULL, 10, NULL, NULL, 0, NULL, NULL,
'介接系統:<br />作業名稱:出貨接收回覆作業<br />發生時間:2015/10/20 15:48:11<br />Message:com.jcraft.jsch.JSchException:
java.net.ConnectException: Connection timed out: connect<br />', '10/20/2015 15:48:11.657')
 {executed in 1 msec}
15:48:12,968  INFO [schedulerFactoryBean_Worker-3] (jdbc.connection:556) - 41. Connection closed


2015年8月5日 星期三

使用PhantomJS實作網頁截圖功能2

承上篇(使用PhantomJS實作網頁截圖功能)

是之前rasterize.js產生網頁截圖時會在指定的路徑下產生實體檔案,但有些情況下也許不希望有這些圖片檔案產生(而希望直接render到網頁中)。此時就需要改寫原本的script。

關鍵API

使用renderBase64()的方法會把原本要產生的圖片用base64的編碼方式轉成字串,可以列印至 console,伺服器端的程式再去讀此字串並以base64解碼。

page.renderBase64('PNG');

改寫網頁截圖script

改寫PhantomJS附的網頁截圖sample code,放在PhantomJS目錄下的examples/rasterize.js:
var page = require('webpage').create(),
    system = require('system'),
    address, output, size;

if (system.args.length < 3 || system.args.length > 5) {
    console.log('Usage: screenshot.js URL filename [imagewidth*imageheight|paperwidth*paperheight|paperformat] [zoom]');
    console.log('  image (png output) examples: "1024*768"');
    console.log('  paper (pdf output) examples: "5in*7.5in", "10cm*20cm", "A4", "Letter"');
    phantom.exit(1);
} else {
    address = system.args[1];
    output = system.args[2];
    var clipRect = null;
 
    page.viewportSize = { width: 600, height: 600 };
    if (system.args.length > 3) {
        size = system.args[3].split('*');
     
        clipRect = {top: 0, left:0, width: size[0], height: size[1]};
    }
    if (system.args.length > 4) {
        page.zoomFactor = system.args[4];
    }
 
    page.open(address, function (status) {
        if (status !== 'success') {
            console.log('Unable to load the address!');
            phantom.exit();
        } else {
            window.setTimeout(function () {              
                try {
                    if (clipRect) {
                        page.clipRect = clipRect;
                    }

                    var base64 = page.renderBase64(output);
                    console.log(base64);
                } catch (e) {
                    console.log('Failed to capture screenshot as ' + output + ': ' + e, "error");
                }
             
                phantom.exit();
            }, 200);
        }
    });
}

伺服機端(Java Servlet)程式

       try {
            long startTime = System.currentTimeMillis();
           
            String targetUrl = "http://www.google.com.tw/";
            String phantomExeFileName ="C:/application/phantomjs-1.9.7/phantomjs.exe";
            String phantomJsFileName ="C:/application/phantomjs-1.9.7/myJs/screenshotBase64.js";
            
            File phantomExeFile = new File(phantomExeFileName);
            File phantomJsFile = new File(phantomJsFileName);
            String imageSize = "1024*768";
           
                String execCommand = String.format("%s %s %s %s %s",
                            phantomExeFile.getAbsolutePath(),
                            phantomJsFile.getAbsolutePath(),
                            (targetUrl == null) || (targetUrl.length() ==0) ? "": targetUrl,
                            "png",
                             imageSize
                        ).trim();
               
                System.out.println(String.format("execCommand = [%s]", execCommand));
               
              Scanner scanner = new Scanner(Runtime.getRuntime().exec(execCommand).getInputStream()).useDelimiter("\\A");
              String cmdout =  scanner.hasNext() ? scanner.next() : "";
              System.out.println(String.format("cmdout = [%s]", cmdout));
             
                byte[] outImg = null;
               
                if ((cmdout != null) && (cmdout.length() > 0)) {
                    outImg = Base64.decodeBase64(outImg);
                }
                 
                OutputStream out = resp.getOutputStream();
               
                if(outImg != null) {
                    InputStream is = new ByteArrayInputStream(outImg);
                    resp.setContentType("image/png");
                    resp.setContentLength(outImg.length);
                    byte[] buf = new byte[4096];
                    while(true) {
                        int r = is.read(buf, 0, buf.length);
                        if(r == -1) break;
                        out.write(buf, 0, r);
                    }
                    is.close();
                    out.flush();
                }             

                long endTime = System.currentTimeMillis();
                System.out.println("total "new Double((endTime - startTime) / 1000) + " seconds");
               
        } catch (Exception e) {
            logger.error("main() - exception: ", e);
        }
       

        return resp;


2015年5月28日 星期四

如何取得 Quartz 目前正在執行的Jobs?

緣起

筆者經常使用Quartz(http://quartz-scheduler.org/)作為排程工作的framework。目前在為這部分實作後台管理與監控的頁面時遇到此問題:「如何取得 Quartz 目前正在執行的Jobs?」

範例程式

    protected void executeInternal(JobExecutionContext ctx) throws JobExecutionException {
        try {
            List<JobExecutionContext> jobsList = ctx.getScheduler().getCurrentlyExecutingJobs();

            for (JobExecutionContext context: jobsList) {
                if ((context != null) && (context.getJobDetail() != null) && (context.getJobDetail().getKey() != null)) {
                    String currentJobName = context.getJobDetail().getKey().getName();
                    logger.info("currentJobName = " + currentJobName);
                    }
                }
            }
        } catch (SchedulerException ex) {
            logger.error("executeInternal(JobExecutionContext) - exception: ", ex);
        }
    }