View | Details | Raw Unified | Return to bug 46252
Collapse All | Expand All

(-)java/org/apache/catalina/valves/AccessLogValve.java (-18 / +77 lines)
Lines 21-30 Link Here
21
21
22
import java.io.BufferedWriter;
22
import java.io.BufferedWriter;
23
import java.io.File;
23
import java.io.File;
24
import java.io.FileWriter;
24
import java.io.FileOutputStream;
25
import java.io.IOException;
25
import java.io.IOException;
26
import java.io.OutputStreamWriter;
26
import java.io.PrintWriter;
27
import java.io.PrintWriter;
28
import java.io.UnsupportedEncodingException;
27
import java.net.InetAddress;
29
import java.net.InetAddress;
30
import java.nio.charset.Charset;
28
import java.text.SimpleDateFormat;
31
import java.text.SimpleDateFormat;
29
import java.util.ArrayList;
32
import java.util.ArrayList;
30
import java.util.Date;
33
import java.util.Date;
Lines 47-52 Link Here
47
import org.apache.coyote.RequestInfo;
50
import org.apache.coyote.RequestInfo;
48
import org.apache.juli.logging.Log;
51
import org.apache.juli.logging.Log;
49
import org.apache.juli.logging.LogFactory;
52
import org.apache.juli.logging.LogFactory;
53
import org.apache.tomcat.util.buf.B2CConverter;
50
54
51
55
52
/**
56
/**
Lines 301-308 Link Here
301
     * Date format to place in log file name. Use at your own risk!
305
     * Date format to place in log file name. Use at your own risk!
302
     */
306
     */
303
    protected String fileDateFormat = null;
307
    protected String fileDateFormat = null;
304
    
308
305
    /**
309
    /**
310
     * Character set used by the log file. If it is <code>null</code>, the
311
     * system default character set will be used. An empty string will be
312
     * treated as <code>null</code> when this property is assigned.
313
     */
314
    protected String encoding = null;
315
316
    /**
306
     * Array of AccessLogElement, they will be used to make log message.
317
     * Array of AccessLogElement, they will be used to make log message.
307
     */
318
     */
308
    protected AccessLogElement[] logElements = null;
319
    protected AccessLogElement[] logElements = null;
Lines 522-527 Link Here
522
        this.fileDateFormat =  fileDateFormat;
533
        this.fileDateFormat =  fileDateFormat;
523
    }
534
    }
524
535
536
    /**
537
     * Return the character set name that is used to write the log file.
538
     *
539
     * @return Character set name, or <code>null</code> if the system default
540
     *  character set is used.
541
     */
542
    public String getEncoding() {
543
        return encoding;
544
    }
545
546
    /**
547
     * Set the character set that is used to write the log file.
548
     * 
549
     * @param encoding The name of the character set.
550
     */
551
    public void setEncoding(String encoding) {
552
        if (encoding != null && encoding.length() > 0) {
553
            this.encoding = encoding;
554
        } else {
555
            this.encoding = null;
556
        }
557
    }
558
525
    // --------------------------------------------------------- Public Methods
559
    // --------------------------------------------------------- Public Methods
526
560
527
    /**
561
    /**
Lines 597-603 Link Here
597
            try {
631
            try {
598
                holder.renameTo(new File(newFileName));
632
                holder.renameTo(new File(newFileName));
599
            } catch (Throwable e) {
633
            } catch (Throwable e) {
600
                log.error("rotate failed", e);
634
                log.error(sm.getString("accessLogValve.rotateFail"), e);
601
            }
635
            }
602
636
603
            /* Make sure date is correct */
637
            /* Make sure date is correct */
Lines 667-673 Link Here
667
                    try {
701
                    try {
668
                        close();
702
                        close();
669
                    } catch (Throwable e) {
703
                    } catch (Throwable e) {
670
                        log.info("at least this wasn't swallowed", e);
704
                        log.info(sm.getString("accessLogValve.closeFail"), e);
671
                    }
705
                    }
672
706
673
                    /* Make sure date is correct */
707
                    /* Make sure date is correct */
Lines 717-742 Link Here
717
        File dir = new File(directory);
751
        File dir = new File(directory);
718
        if (!dir.isAbsolute())
752
        if (!dir.isAbsolute())
719
            dir = new File(System.getProperty("catalina.base"), directory);
753
            dir = new File(System.getProperty("catalina.base"), directory);
720
        dir.mkdirs();
754
        if (!dir.exists()) {
755
            if (!dir.mkdirs()) {
756
                log.error(sm.getString("accessLogValve.openDirFail", dir));
757
            }
758
        }
721
759
722
        // Open the current log file
760
        // Open the current log file
761
        File pathname;
762
        // If no rotate - no need for dateStamp in fileName
763
        if (rotatable) {
764
            pathname = new File(dir.getAbsoluteFile(), prefix + dateStamp
765
                    + suffix);
766
        } else {
767
            pathname = new File(dir.getAbsoluteFile(), prefix + suffix);
768
        }
769
        File parent = pathname.getParentFile();
770
        if (!parent.exists()) {
771
            if (!parent.mkdirs()) {
772
                log.error(sm.getString("accessLogValve.openDirFail", parent));
773
            }
774
        }
775
776
        Charset charset = null;
777
        if (encoding != null) {
778
            try {
779
                charset = B2CConverter.getCharset(encoding);
780
            } catch (UnsupportedEncodingException ex) {
781
                log.error(sm.getString(
782
                        "accessLogValve.unsupportedEncoding", encoding), ex);
783
            }
784
        }
785
        if (charset == null) {
786
            charset = Charset.defaultCharset();
787
        }
788
723
        try {
789
        try {
724
            String pathname;
790
            writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(
725
            // If no rotate - no need for dateStamp in fileName
791
                    new FileOutputStream(pathname, true), charset), 128000),
726
            if (rotatable) {
792
                    false);
727
                pathname = dir.getAbsolutePath() + File.separator + prefix
793
728
                        + dateStamp + suffix;
794
            currentLogFile = pathname;
729
            } else {
730
                pathname = dir.getAbsolutePath() + File.separator + prefix
731
                        + suffix;
732
            }
733
            writer = new PrintWriter(new BufferedWriter(new FileWriter(
734
                    pathname, true), 128000), false);
735
            
736
            currentLogFile = new File(pathname);
737
        } catch (IOException e) {
795
        } catch (IOException e) {
738
            writer = null;
796
            writer = null;
739
            currentLogFile = null;
797
            currentLogFile = null;
798
            log.error(sm.getString("accessLogValve.openFail", pathname), e);
740
        }
799
        }
741
    }
800
    }
742
 
801
 
(-)java/org/apache/catalina/valves/LocalStrings.properties (+5 lines)
Lines 30-35 Link Here
30
# Access log valve
30
# Access log valve
31
accessLogValve.alreadyStarted=Access Logger has already been started
31
accessLogValve.alreadyStarted=Access Logger has already been started
32
accessLogValve.notStarted=Access Logger has not yet been started
32
accessLogValve.notStarted=Access Logger has not yet been started
33
accessLogValve.openFail=Failed to open access log file [{0}]
34
accessLogValve.closeFail=Failed to close access log file
35
accessLogValve.openDirFail=Failed to create directory [{0}] for access logs
36
accessLogValve.rotateFail=Failed to rotate access log
37
accessLogValve.unsupportedEncoding=Failed to set encoding to [{0}], will use the system default character set.
33
38
34
# Error report valve
39
# Error report valve
35
errorReportValve.errorReport=Error report
40
errorReportValve.errorReport=Error report
(-)java/org/apache/juli/FileHandler.java (-2 / +6 lines)
Lines 364-371 Link Here
364
        // Open the current log file
364
        // Open the current log file
365
        writerLock.writeLock().lock();
365
        writerLock.writeLock().lock();
366
        try {
366
        try {
367
            String pathname = dir.getAbsolutePath() + File.separator +
367
            File pathname = new File(dir.getAbsoluteFile(), prefix
368
                prefix + (rotatable ? date : "")  + suffix;
368
                    + (rotatable ? date : "") + suffix);
369
            File parent = pathname.getParentFile();
370
            if (!parent.exists()) {
371
                parent.mkdirs();
372
            }
369
            String encoding = getEncoding();
373
            String encoding = getEncoding();
370
            FileOutputStream fos = new FileOutputStream(pathname, true);
374
            FileOutputStream fos = new FileOutputStream(pathname, true);
371
            OutputStream os = bufferSize>0?new BufferedOutputStream(fos,bufferSize):fos;
375
            OutputStream os = bufferSize>0?new BufferedOutputStream(fos,bufferSize):fos;
(-)webapps/docs/config/valve.xml (+7 lines)
Lines 103-108 Link Here
103
        (relative to $CATALINA_BASE).</p>
103
        (relative to $CATALINA_BASE).</p>
104
      </attribute>
104
      </attribute>
105
105
106
      <attribute name="encoding" required="false">
107
        <p>Character set used to write the log file. An empty string means
108
        to use the system default character set. Default value: use the
109
        system default character set.
110
        </p>
111
      </attribute>
112
106
      <attribute name="pattern" required="false">
113
      <attribute name="pattern" required="false">
107
        <p>A formatting layout identifying the various information fields
114
        <p>A formatting layout identifying the various information fields
108
        from the request and response to be logged, or the word
115
        from the request and response to be logged, or the word

Return to bug 46252