简体   繁体   中英

Logging of file with java.util.logging and restlet

I've found a lot of good guides that explain how to use java.util.logging, but anyway I'm doing something wrong because I can't come up to make the system load to my logging.properties file. Further notice is that I'm using also Restlet framework that used the logger and made it usable from their classes.

fist thing I setup the system property to my properties file:

    /**
 * Tells the file configuration for the JVM logger
 */
static void setupLoggerProperties(){
    System.setProperty("java.util.logging.config.file",
            "logging.properties");

    //Test
    LOGGER.info("Test info");
    LOGGER.fine("Test Fine");
    LOGGER.finer("Test FINER");
    LOGGER.warning("WARNING");
}

And then I test the thing, content of my properties file is:

    ############################################################
#   Default Logging Configuration File
#
# You can use a different file by specifying a filename
# with the java.util.logging.config.file system property.  
# For example java -Djava.util.logging.config.file=myfile
############################################################

############################################################
#   Global properties
############################################################

# "handlers" specifies a comma separated list of log Handler 
# classes.  These handlers will be installed during VM startup.
# Note that these classes must be on the system classpath.
# By default we only configure a ConsoleHandler, which will only
# show messages at the INFO and above levels.
handlers= java.util.logging.FileHandler, java.util.logging.ConsoleHandler

# To also add the FileHandler, use the following line instead.
#handlers= java.util.logging.FileHandler, java.util.logging.ConsoleHandler

# Default global logging level.
# This specifies which kinds of events are logged across
# all loggers.  For any given facility this global level
# can be overriden by a facility specific level
# Note that the ConsoleHandler also has a separate level
# setting to limit messages printed to the console.

    # ------------------
    # Loggers properties
    # ------------------

    .level=ALL
    org.mortbay.level=WARNING
    org.restlet.level=WARNING
    com.noelios.level=WARNING


    # -------------------------
    # ConsoleHandler properties
    # -------------------------

    # Specifies the default level for the Handler  (defaults to Level.INFO).
    java.util.logging.ConsoleHandler.level=ALL

    # Specifies the name of a Filter class to use (defaults to no Filter).
    # java.util.logging.ConsoleHandler.filter=

    # Specifies the name of a Formatter class to use (defaults to java.util.logging.SimpleFormatter).
    # java.util.logging.ConsoleHandler.formatter=

    # The name of the character set encoding to use (defaults to the default platform encoding).
    # java.util.logging.ConsoleHandler.encoding=

    # ------------------------------
    # General FileHandler properties
    # ------------------------------

    # Specifies the default level for the Handler  (defaults to Level.ALL).
    # java.util.logging.FileHandler.level=ALL

    # Specifies the name of a Filter class to use (defaults to no Filter).
    # java.util.logging.FileHandler.filter=

    # Specifies the name of a Formatter class to use (defaults to java.util.logging.XMLFormatter)
    java.util.logging.FileHandler.formatter=java.util.logging.SimpleFormatter

    # The name of the character set encoding to use (defaults to the default platform encoding).
    # java.util.logging.FileHandler.encoding=

    # Specifies an approximate maximum amount to write (in bytes) to any one file.
    # If this is zero, then there is no limit. (Defaults to no limit).
    java.util.logging.FileHandler.limit=10000000

    # Specifies how many output files to cycle through (defaults to 1).
    java.util.logging.FileHandler.count=100

    # Specifies a pattern for generating the output file name. (Defaults to "%h/java%u.log").
    # A pattern consists of a string that includes the following special components that will be replaced at runtime:
    #    "/" the local pathname separator
    #    "%t" the system temporary directory
    #    "%h" the value of the "user.home" system property
    #    "%g" the generation number to distinguish rotated logs
    #    "%u" a unique number to resolve conflicts
    #    "%%" translates to a single percent sign "%"
    java.util.logging.FileHandler.pattern=logs/log-%u-%g.log

    # Specifies whether the FileHandler should append onto any existing files (defaults to false).
    # java.util.logging.FileHandler.append=

# Example to customize the SimpleFormatter output format 
# to print one-line log message like this:
#     <level>: <log message> [<date/time>]
#
# java.util.logging.SimpleFormatter.format=%4$s: %5$s [%1$tc]%n

############################################################
# Facility specific properties.
# Provides extra control for each logger.
############################################################

# For example, set the com.xyz.foo logger to only log SEVERE
# messages:
#com.xyz.foo.level = SEVERE

Result is that I've no file written in the /logs folder and the console only prints the WARNING and INFO strings.

It might be really something stupid I'm missing out, so please somebody could give me an hand? :)

You can call LogManager.readConfiguration() after you set the system property to load the settings. However, you should really try to set property as a system property on startup as described in the comments:

# You can use a different file by specifying a filename
# with the java.util.logging.config.file system property.  
# For example java -Djava.util.logging.config.file=myfile

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM