OSDN Git Service

メール機能:ログファイルへの出力
authorhayashi <yuu.hayashi@deister.jp>
Sun, 18 Dec 2011 23:46:00 +0000 (08:46 +0900)
committerhayashi <yuu.hayashi@deister.jp>
Sun, 18 Dec 2011 23:46:00 +0000 (08:46 +0900)
src/hayashi/yuu/tools/logger/LoggerFactory.java
src/hayashi/yuu/tools/mail/SendMail.java

index 2fb9080..c806b78 100644 (file)
@@ -4,9 +4,11 @@ import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.UnsupportedEncodingException;
+import java.util.logging.FileHandler;
 import java.util.logging.LogManager;
 import java.util.logging.Logger;
 
+
 /**
  * ロギングファイルに動作ログを出力する簡単なモデル
  * "log"+日時+".log"ファイルに出力される。
@@ -25,61 +27,73 @@ import java.util.logging.Logger;
  * @version 2010/02/07
  * @since 2010/02/07
  */
-public abstract class LoggerFactory 
+public abstract class LoggerFactory
 {
        public static void main(String[] args) {
                /*
                 * (設定例)
                 *              handlers=java.util.logging.ConsoleHandler, java.util.logging.FileHandler
                 *              .level=FINEST
-                * 
+                *
                 *              java.util.logging.ConsoleHandler.level=FINEST
                 *              java.util.logging.ConsoleHandler.formatter=hayashi.yuu.tools.logger.YuuLogFormatter
-                * 
+                *
                 *              java.util.logging.FileHandler.level=WARNING
                 *              java.util.logging.FileHandler.pattern=SampleLogging%u.%g.log
                 *              java.util.logging.FileHandler.formatter=hayashi.yuu.tools.logger.YuuLogFormatter
                 *              java.util.logging.FileHandler.count=10
-                *              
-                * 
+                *
+                *
                 * 標準設定時でのログ出力。
                 * info、warning、severeの3つのレベルのみ標準エラー出力に出力されます。
                 * また、同時にファイルへも出力します。
                 * 出力先ファイルは「Logging%u.%g.txt」。ログファイルは10個でローテーションする。
-                * 
+                *
                 * 情報: [info] 情報
                 * 警告: [warning] 警告
                 * 致命的: [severe] 致命的
                 */
-               Logger logger = LoggerFactory.getInstance();
-               logger.finest("[finest] 詳細レベル(高)");
-               logger.finer("[finer] 詳細レベル(中)");
-               logger.fine("[fine] 詳細レベル(小)");
-               logger.config("[config] 設定");
-               logger.info("[info] 情報");
-               logger.warning("[warning] 警告");
-               logger.severe("[severe] 致命的");
+               Logger logger;
+               try {
+                       logger = LoggerFactory.getInstance();
+                       logger.finest("[finest] 詳細レベル(高)");
+                       logger.finer("[finer] 詳細レベル(中)");
+                       logger.fine("[fine] 詳細レベル(小)");
+                       logger.config("[config] 設定");
+                       logger.info("[info] 情報");
+                       logger.warning("[warning] 警告");
+                       logger.severe("[severe] 致命的");
+               }
+               catch (Exception e) {
+                       e.printStackTrace();
+               }
        }
-       
-    public static Logger logger;
-    
-    /**
-     * 簡単な標準ロガーを得る
-     * @return 標準ロガー
-     */
-    public static Logger getInstance() {
-       if (logger == null) {
-            //FileHandler handler = new FileHandler("GuardixMonitor.log", 1000, 3);    // ハンドラオブジェクトの生成
-            //handler.setFormatter(new SimpleFormatter());             // 出力フォーマットの指定
-            logger = Logger.getLogger("log");          // Loggerオブジェクトの生成
-            //GuardixMonitor.logger.addHandler(handler);               // Loggerオブジェクトにハンドラを追加
-       }
+
+       public static Logger logger;
+
+       /**
+        * 簡単な標準ロガーを得る
+        * @return      標準ロガー
+        */
+       public static Logger getInstance() {
+               if (logger == null) {
+                       // ハンドラオブジェクトの生成
+                       FileHandler handler;
+                       try {
+                               handler = new FileHandler("Logging", 1000, 3);
+                               handler.setFormatter(new YuuLogFormatter());                            // 出力フォーマットの指定
+                               logger = Logger.getLogger("log");                                                       // Loggerオブジェクトの生成
+                               logger.addHandler(handler);                                                                     // Loggerオブジェクトにハンドラを追加
+                       } catch (IOException e) {
+                               e.printStackTrace();
+                       }
+               }
                return logger;
-    }
-       
+       }
+
     /**
         * ログ設定プロパティファイルのファイル内容
-        * 
+        *
         */
        protected static final String LOGGING_PROPERTIES_DATA
            = "handlers=java.util.logging.ConsoleHandler, java.util.logging.FileHandler\n"
@@ -91,7 +105,6 @@ public abstract class LoggerFactory
            + "java.util.logging.FileHandler.formatter=hayashi.yuu.tools.logger.YuuLogFormatter\n"
            + "java.util.logging.FileHandler.count=10";
 
-    
     /**
         * static initializer によるログ設定の初期化
         */
@@ -118,6 +131,6 @@ public abstract class LoggerFactory
             catch (IOException e) {
                 logger.warning("ログ設定: ログ設定プロパティファイルのストリームクローズ時に例外が発生しました。:" + e.toString());
             }
-        } 
+        }
     }
 }
index fe2553c..e065217 100644 (file)
@@ -24,7 +24,7 @@ import hayashi.yuu.tools.mail.SiteData;
  * <p>Eメールを送信するためのクラス<br/>
  * JavaMailを使用する</p>
  * @see <a href="http://java.sun.com/products/javamail/javadocs/index.html">JavaMail API</a>
- * 
+ *
  * @author hayashi
  * @version 2010/02/05 SMTPサーバーのポート番号指定を追加
  */
@@ -36,7 +36,7 @@ public class SendMail
                        SendMail mail = new SendMail(siteData);
             mail.setSubject("[SendMail] SendMail テスト");
             mail.setContent("本文\n");
-            mail.setAttachment(new File("activation-1.1.jar"));
+            mail.setAttachment(new File("attache.jpg"));
             mail.send();
         }
         catch (Exception e) {
@@ -52,10 +52,10 @@ public class SendMail
     protected String content;             // プロパティ 本文(content) の値
     protected String mailHost;            // プロパティ SMTP(mail.smtp.host) の値
     protected String mailHostPort;        // プロパティ SMTP_PORT(mail.smtp.port) の値
-    
+
     protected Vector<File> attachments = new Vector<File>();   // 添付ファイル
     protected Vector<String> attachNames = new Vector<String>();               // 添付ファイル名
-    
+
     protected boolean POP_before_SMTP = false;
     protected boolean STARTTLS = false;
     protected boolean USER_AUTH = false;
@@ -63,9 +63,9 @@ public class SendMail
     protected String userId = "";
     protected String password ="";
     protected Session session = null;
-    
-    /** 
-     * 新しい YuuMail インスタンスを作成する 
+
+    /**
+     * 新しい YuuMail インスタンスを作成する
      * @param mailProp メール設定ファイルのパス名称
      */
     public SendMail(String mailProp) {
@@ -73,12 +73,12 @@ public class SendMail
     }
 
     /**
-     * 新しい インスタンスを作成する 
+     * 新しい インスタンスを作成する
      * @param siteData メール設定値を包含したオブジェクト
      */
     public SendMail(SiteData siteData) {
         super();
-        
+
         if (checkItem(siteData.MAIL_SMTP)) {
             this.setMailHost(siteData.MAIL_SMTP);
         }
@@ -104,7 +104,7 @@ public class SendMail
         this.popServer = siteData.MAIL_POP;
         this.userId = siteData.USER_ID;
         this.password = siteData.PASSWORD;
-        
+
         // メール(MimeMessageオブジェクト)を生成する
         Properties props = System.getProperties();
         props.put("mail.smtp.host", getMailHost());
@@ -126,24 +126,24 @@ public class SendMail
        }
        return true;
     }
-    
-    /** 
+
+    /**
      * プロパティ mailHost の取得メソッド。
      * @return プロパティ mailHost の値。
      */
     public String getMailHost() {
         return this.mailHost;
     }
-    
-    /** 
+
+    /**
      * SMTPサーバーを設定
      *  @param mailHost SMTPサーバー名またはSMTPサーバーのIPアドレス
     */
     public void setMailHost(String mailHost) {
         this.mailHost = mailHost;
     }
-    
-    /** 
+
+    /**
      * プロパティ SMTPポート番号 の取得メソッド。
      * @return SMTPポート番号を表す文字列(デシマル)
      */
@@ -151,14 +151,14 @@ public class SendMail
         return this.mailHostPort;
     }
 
-    /** 
+    /**
      * SMTPポート番号を設定
      *  @param portStr SMTPポート番号を表す文字列(デシマル)
      */
     public void setMailPort(String portStr) {
         this.mailHostPort = portStr;
     }
-    
+
     /**
      * 送信先(To)を設定.数設定可
      * @param to 送信先(To)
@@ -168,7 +168,7 @@ public class SendMail
             toVector.addElement(to);
         }
     }
-    
+
     /**
      * 送信先(Cc)を設定。複数設定可
      * @param cc 送信先(cc)
@@ -178,7 +178,7 @@ public class SendMail
             ccVector.addElement(cc);
         }
     }
-    
+
     /**
      * 送信先(Bco)を設定.複数選択可
      * @param bcc 送信先(Bcc)
@@ -188,31 +188,31 @@ public class SendMail
             bccVector.addElement(bcc);
         }
     }
-    
-    /** 
+
+    /**
      * プロパティ from の取得メソッド。
      * @return プロパティ from の値。
      */
     public String getFrom() {
         return from;
     }
-    
-    /** 
+
+    /**
      * プロパティ 送信元(from) の設定メソッド。
      * @param from 送信元メールアドレス
      */
     public void setFrom(String from) {
         this.from = from;
     }
-    
-    /** 
+
+    /**
      * プロパティ subject の取得メソッド。
      * @return プロパティ subject の値。
      */
     public String getSubject() {
         return subject;
     }
-    
+
     /**
      * プロパティ 題名(subject) の設定メソッド。
      * メールタイトル(題名)を設定
@@ -221,16 +221,16 @@ public class SendMail
     public void setSubject(String subject) {
         this.subject = subject;
     }
-    
-    /** 
+
+    /**
      * 本文(content)の取得メソッド。
      * @return 本文の値。
      */
     public String getContent() {
         return this.content;
     }
-    
-    /** 
+
+    /**
      * 本文(content)の設定メソッド。
      * 本文(content)を設定
      * @param content 本文(content)
@@ -238,8 +238,8 @@ public class SendMail
     public void setContent(String content) {
         this.content = content;
     }
-    
-    /** 
+
+    /**
      * 添付ファイルの設定メソッド。
      * 添付するファイル(attachment)を設定.添付ファイルは複数設定可。
      * @param attachment 添付するファイル(attachment)
@@ -250,8 +250,8 @@ public class SendMail
         attachments.addElement(attachment);
         attachNames.addElement(attachment.getName());
     }
-    
-    /** 
+
+    /**
      * 添付ファイルの設定メソッド。
      * 添付するファイル(attachment)を設定.添付ファイルは複数設定可。
      * @param attachment 添付するファイル(attachment)
@@ -263,7 +263,7 @@ public class SendMail
          attachments.addElement(attachment);
          attachNames.addElement(name);
      }
-     
+
     /**
      * メールを送信
      * @throws MessagingException エラー:メールの送信に失敗しました。
@@ -284,7 +284,7 @@ public class SendMail
         }
 
         MimeMessage message = createMessage();
-        
+
         if (this.USER_AUTH) {
                System.out.println("transport.connect(null, '"+ userId +"', '"+ password+"')");
             Transport transport = this.session.getTransport("smtp");
@@ -299,12 +299,12 @@ public class SendMail
                System.out.println("no SMTP_AUTH!");
                Transport.send(message);
         }
-        
+
         if (this.POP_before_SMTP == true) {
             store.close();
         }
     }
-    
+
     /**
      * 新規にメールインスタンスを生成する。
      * @return メールインスタンス
@@ -313,7 +313,7 @@ public class SendMail
      */
     protected MimeMessage createMessage() throws MessagingException, UnsupportedEncodingException {
         MimeMessage msg = new MimeMessage(this.session);
-        
+
         // 送信先(To)
         InternetAddress[] toList = new InternetAddress[toVector.size()];
         for (int i=0; i < toVector.size(); i++) {
@@ -334,7 +334,7 @@ public class SendMail
             bccList[i] = new InternetAddress(bccVector.elementAt(i));
         }
         msg.setRecipients(Message.RecipientType.BCC, bccList);
-        
+
         // 送信元(From)
         msg.setFrom(new InternetAddress(getFrom()));
 
@@ -344,7 +344,7 @@ public class SendMail
         // 本文(Content)
         MimeMultipart naiyou = new MimeMultipart();
         msg.setContent(naiyou);
-        
+
         MimeBodyPart honbun = new MimeBodyPart();
         honbun.setContent(getContent(), "text/plain; charset=\"iso-2022-jp\"");
         naiyou.addBodyPart(honbun);
@@ -359,7 +359,7 @@ public class SendMail
             tenpu.setFileName(attachNames.elementAt(i));
             naiyou.addBodyPart(tenpu);
         }
-        
+
         return msg;
     }
 }