Example usage for java.util.logging SimpleFormatter SimpleFormatter

List of usage examples for java.util.logging SimpleFormatter SimpleFormatter

Introduction

In this page you can find the example usage for java.util.logging SimpleFormatter SimpleFormatter.

Prototype

SimpleFormatter

Source Link

Usage

From source file:GIST.IzbirkomExtractor.IzbirkomExtractor.java

/**
 * @param args//from   w w w . j  a v a  2s. c  om
 */
public static void main(String[] args) {

    // process command-line options
    Options options = new Options();
    options.addOption("n", "noaddr", false, "do not do any address matching (for testing)");
    options.addOption("i", "info", false, "create and populate address information table");
    options.addOption("h", "help", false, "this message");

    // database connection
    options.addOption("s", "server", true, "database server to connect to");
    options.addOption("d", "database", true, "OSM database name");
    options.addOption("u", "user", true, "OSM database user name");
    options.addOption("p", "pass", true, "OSM database password");

    // logging options
    options.addOption("l", "logdir", true, "log file directory (default './logs')");
    options.addOption("e", "loglevel", true, "log level (default 'FINEST')");

    // automatically generate the help statement
    HelpFormatter help_formatter = new HelpFormatter();

    // database URI for connection
    String dburi = null;

    // Information message for help screen
    String info_msg = "IzbirkomExtractor [options] <html_directory>";

    try {
        CommandLineParser parser = new GnuParser();
        CommandLine cmd = parser.parse(options, args);

        if (cmd.hasOption('h') || cmd.getArgs().length != 1) {
            help_formatter.printHelp(info_msg, options);
            System.exit(1);
        }

        /* prohibit n and i together */
        if (cmd.hasOption('n') && cmd.hasOption('i')) {
            System.err.println("Options 'n' and 'i' cannot be used together.");
            System.exit(1);
        }

        /* require database arguments without -n */
        if (cmd.hasOption('n')
                && (cmd.hasOption('s') || cmd.hasOption('d') || cmd.hasOption('u') || cmd.hasOption('p'))) {
            System.err.println("Options 'n' and does not need any databse parameters.");
            System.exit(1);
        }

        /* require all 4 database options to be used together */
        if (!cmd.hasOption('n')
                && !(cmd.hasOption('s') && cmd.hasOption('d') && cmd.hasOption('u') && cmd.hasOption('p'))) {
            System.err.println(
                    "For database access all of the following arguments have to be specified: server, database, user, pass");
            System.exit(1);
        }

        /* useful variables */
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'kk:mm");
        String dateString = formatter.format(new Date());

        /* setup logging */
        File logdir = new File(cmd.hasOption('l') ? cmd.getOptionValue('l') : "logs");
        FileUtils.forceMkdir(logdir);
        File log_file_name = new File(
                logdir + "/" + IzbirkomExtractor.class.getName() + "-" + formatter.format(new Date()) + ".log");
        FileHandler log_file = new FileHandler(log_file_name.getPath());

        /* create "latest" link to currently created log file */
        Path latest_log_link = Paths.get(logdir + "/latest");
        Files.deleteIfExists(latest_log_link);
        Files.createSymbolicLink(latest_log_link, Paths.get(log_file_name.getName()));

        log_file.setFormatter(new SimpleFormatter());
        LogManager.getLogManager().reset(); // prevents logging to console
        logger.addHandler(log_file);
        logger.setLevel(cmd.hasOption('e') ? Level.parse(cmd.getOptionValue('e')) : Level.FINEST);

        // open directory with HTML files and create file list
        File dir = new File(cmd.getArgs()[0]);
        if (!dir.isDirectory()) {
            System.err.println("Unable to find directory '" + cmd.getArgs()[0] + "', exiting");
            System.exit(1);
        }
        PathMatcher pmatcher = FileSystems.getDefault()
                .getPathMatcher("glob:?  * ?*.html");
        ArrayList<File> html_files = new ArrayList<>();
        for (Path file : Files.newDirectoryStream(dir.toPath()))
            if (pmatcher.matches(file.getFileName()))
                html_files.add(file.toFile());
        if (html_files.size() == 0) {
            System.err.println("No matching HTML files found in '" + dir.getAbsolutePath() + "', exiting");
            System.exit(1);
        }

        // create csvResultSink
        FileOutputStream csvout_file = new FileOutputStream("parsed_addresses-" + dateString + ".csv");
        OutputStreamWriter csvout = new OutputStreamWriter(csvout_file, "UTF-8");
        ResultSink csvResultSink = new CSVResultSink(csvout, new CSVStrategy('|', '"', '#'));

        // Connect to DB and osmAddressMatcher
        AddressMatcher osmAddressMatcher;
        DBSink dbSink = null;
        DBInfoSink dbInfoSink = null;
        if (cmd.hasOption('n')) {
            osmAddressMatcher = new DummyAddressMatcher();
        } else {
            dburi = "jdbc:postgresql://" + cmd.getOptionValue('s') + "/" + cmd.getOptionValue('d');
            Connection con = DriverManager.getConnection(dburi, cmd.getOptionValue('u'),
                    cmd.getOptionValue('p'));
            osmAddressMatcher = new OsmAddressMatcher(con);
            dbSink = new DBSink(con);
            if (cmd.hasOption('i'))
                dbInfoSink = new DBInfoSink(con);
        }

        /* create resultsinks */
        SinkMultiplexor sm = SinkMultiplexor.newSinkMultiplexor();
        sm.addResultSink(csvResultSink);
        if (dbSink != null) {
            sm.addResultSink(dbSink);
            if (dbInfoSink != null)
                sm.addResultSink(dbInfoSink);
        }

        // create tableExtractor
        TableExtractor te = new TableExtractor(osmAddressMatcher, sm);

        // TODO: printout summary of options: processing date/time, host, directory of HTML files, jdbc uri, command line with parameters

        // iterate through files
        logger.info("Start processing " + html_files.size() + " files in " + dir);
        for (int i = 0; i < html_files.size(); i++) {
            System.err.println("Parsing #" + i + ": " + html_files.get(i));
            te.processHTMLfile(html_files.get(i));
        }

        System.err.println("Processed " + html_files.size() + " HTML files");
        logger.info("Finished processing " + html_files.size() + " files in " + dir);

    } catch (ParseException e1) {
        System.err.println("Failed to parse CLI: " + e1.getMessage());
        help_formatter.printHelp(info_msg, options);
        System.exit(1);
    } catch (IOException e) {
        System.err.println("I/O Exception: " + e.getMessage());
        e.printStackTrace();
        System.exit(1);
    } catch (SQLException e) {
        System.err.println("Database '" + dburi + "': " + e.getMessage());
        System.exit(1);
    } catch (ResultSinkException e) {
        System.err.println("Failed to initialize ResultSink: " + e.getMessage());
        System.exit(1);
    } catch (TableExtractorException e) {
        System.err.println("Failed to initialize Table Extractor: " + e.getMessage());
        System.exit(1);
    } catch (CloneNotSupportedException | IllegalAccessException | InstantiationException e) {
        System.err.println("Something really odd happened: " + e.getMessage());
        e.printStackTrace();
        System.exit(1);
    }
}

From source file:org.scify.NewSumServer.Server.Utils.Main.java

public static void main(String[] args) throws FeedParserException, NetworkException, IOException {
    //initialize logger
    Handler h;/*from w  w  w  . j ava2s  .  com*/
    try {
        h = new FileHandler(sLogFile);
        SimpleFormatter f = new SimpleFormatter();
        h.setFormatter(f);
        LOGGER.addHandler(h);
        LOGGER.setLevel(Level.FINE);
    } catch (IOException ex) {
        LOGGER.log(Level.SEVERE, ex.getMessage(), ex);
    } catch (SecurityException ex) {
        LOGGER.log(Level.SEVERE, ex.getMessage(), ex);
    }
    //program info
    System.out.print("NewSumServer Switches:\n\n"
            + "-BaseDir: The full path to the folder where the storage module stores data\n"
            + "-PathToSources: The file named RSSSources.txt with it's full path\n"
            + "\t(e.g. /home/pathtosources/RSSSources.txt)\n"
            + "-indexPath: The full path to the folder where the Indexer Class stores data\n"
            + "-SummaryPath: The full path to the folder where the Summarisation package stores data\n"
            + "-ArticlePath: The full path to the folder where the "
            + "Summarisation package stores the Clustered Articles\n"
            + "-ToolPath: The full path to the folder for misc Tools\n"
            + "-iOutputSize: The max number of Sentences the Summariser prints\n"
            + "-ArticleMaxDays: The Number of Max Days to Accept an article (until now)\n"
            + "-useInputDirData: true or false, defaults to false\n"
            + "-DebugRun: true if you want to run with category switching (for quicker runs)\n\n"
            + "Example Usage: java -jar NewSumServer.jar -BaseDir=./data/Dir -iOutputSize=50\n\n");
    //Parse and check Command Line arguments
    parseCommandLine(args);
    //Write Configuration file so that NewSumFreeService reads statics
    writeConfigFile();
    // check if splitterTraining file was changed since the previous run
    if (SplitterTrainingFileChanged()) {
        // if so, delete the Model file, in order to get recreated with the new data
        File sDat = new File(sToolPath + "splitModel.dat");
        try {
            sDat.delete();
            LOGGER.log(Level.INFO, "deleted {0} cause splitterTrainer was updated", sDat.toString());
        } catch (Exception ex) {
            LOGGER.log(Level.WARNING,
                    "Could not delete {0}, although file Was Changed. " + "Please do it manually",
                    sDat.toString());
        }
    }
    //init data storage
    IDataStorage ids = new InsectFileIO(sBaseDir);
    //        justWaitABit(50000);

    //init rssSources 
    RSSSources r = new RSSSources(sPathToSources);
    // initialize sources: read the sources file
    r.initialize(ids);
    //get the sources
    HashMap<String, String> Sources = r.getRssLinks();//link,category
    //get categories
    Collection<String> sCategories = r.getCategories(); // TODO: Ignore UNCLASSIFIED CATEGORY
    ArrayList<String> lCategories = new ArrayList<String>(sCategories);
    //init rssparser
    ISourceParser isp = new RssParser(ids, iArticleDays);
    //DEBUG LINES //get user input
    List al = new ArrayList(sCategories);
    ArrayList<String> subSources = null;
    ArrayList<Article> Articles = new ArrayList<Article>();
    String sCurCateg = "0";
    if (bDebugRun) { // if only one category needed (quick run)
        System.out.println("Choose Category by number: \nIf -1, all categories are loaded");
        for (int i = 0; i < lCategories.size(); i++) {
            System.out.println(String.valueOf(i) + ": " + lCategories.get(i));
        }
        Scanner user_input = new Scanner(System.in);
        sCurCateg = user_input.next();
        /////////////

        if (Integer.valueOf(sCurCateg) != -1) {
            subSources = new ArrayList<String>((HashSet<String>) Utilities.getKeysByValue(Sources,
                    (String) al.get(Integer.valueOf(sCurCateg))));
            //accept all articles from each category
            Articles = (ArrayList<Article>) isp.getAllNewsByCategory(subSources,
                    (String) al.get(Integer.valueOf(sCurCateg)));

        } else if (Integer.valueOf(sCurCateg) == -1) {

            //get all articles
            Articles = (ArrayList<Article>) isp.getAllArticles(Sources);
        }
    } else { //if all categories by default (no user choosing - normal mode)
        Articles = (ArrayList<Article>) isp.getAllArticles(Sources);
    }
    // check for spam sentences
    Utilities.checkForPossibleSpam(Articles);
    //Save Article List to Drive, so that the clusterer loads it
    isp.saveAllArticles(); //Name: "AllArticles", Category: "feeds"
    //        ArticleClusterer ac = new ArticleClusterer(subArticles, ids, sArticlePath);
    //get least occurencies of articles
    //        threshold = Utilities.getLeastOccurencies(Articles);
    //        //Train Classification Module
    //        clm = new classificationModule();
    //
    //
    //        //initialize Distribution category set
    //        Distribution<String> dArticleCategory = new Distribution<String>();
    //
    //        for (int i = 0; i < Articles.size(); i++) {
    //            if (Articles.get(i).getToWrap()) {
    //                boolean mergeGraph = true;
    //                //increase Distribution set 1.0
    //                dArticleCategory.increaseValue(Articles.get(i).getCategory(), 1.0);
    //                //check threshold
    //                double dInstanceCount = dArticleCategory.getValue(Articles.get(i).getCategory());
    //                if (dInstanceCount < threshold) {
    //
    //                    //check mergeGraph threshold --> threshold/2  turn mergeGraph from true to false
    //                    if (dInstanceCount > (threshold / 2)) {
    //                        mergeGraph = false;
    //                    }
    //                    clm.feedClassifier(Articles.get(i).getCategory(), Articles.get(i).getText(), mergeGraph);
    //                }
    //
    //            }
    //        }
    // Initialize Clusterer
    ArticleClusterer ac;

    ac = new ArticleClusterer((ArrayList<Article>) ids.loadObject("AllArticles", "feeds"), ids, sArticlePath);

    // Perform clustering calculations

    ac.calculateClusters(NVSThreshold, SSThreshold);
    //specify the locale for the indexer
    Locale loc = sPathToSources.endsWith("GR.txt") ? new Locale("el") : new Locale("en");
    // Create a new indexer
    Indexer ind = new Indexer(sArticlePath, sindexPath, loc);
    // Create the Index
    try {
        ind.createIndex();
    } catch (CorruptIndexException ex) {
        LOGGER.log(Level.SEVERE, ex.getMessage(), ex);
    } catch (LockObtainFailedException ex) {
        LOGGER.log(Level.SEVERE, ex.getMessage(), ex);
    } catch (IOException ex) {
        LOGGER.log(Level.SEVERE, ex.getMessage(), ex);
    }
    // Init the summarizer and obtain summaries
    INSECTDB idb = new INSECTFileDB("", sSummaryPath);
    Summariser sum = new Summariser(new HashSet<Topic>(ac.getArticlesPerCluster().values()), idb);
    // Perform summarization for all clusters
    Map<String, List<Sentence>> AllSummaries;
    AllSummaries = sum.getSummaries();

    // DEBUG // store summaries
    //        for (Map.Entry mp : AllSummaries.entrySet()) {
    //            String sUID = (String) mp.getKey();
    //            List<Sentence> lsSen = (List<Sentence>) mp.getValue();
    //            if (getNumberOfSources(lsSen) > 1) {
    //                writeSummaryToFile(lsSen, sUID, ac.getArticlesPerCluster());
    //            }
    //        }        
    // DEBUG //
    if (bDebugRun) {

        // DEBUG LINES
        // Delete files in "data/txtSummaries" and write all the summaries extracted
        File f = new File(sTxtSumPath);
        if (f.exists()) {
            for (File k : f.listFiles()) {
                k.delete();
            }
        }
        for (Map.Entry mp : AllSummaries.entrySet()) {
            String sUID = (String) mp.getKey();
            List<Sentence> lsSen = (List<Sentence>) mp.getValue();
            if (getNumberOfSources(lsSen) > 1) {
                writeSummaryToFile(lsSen, sUID, ac.getArticlesPerCluster());
            }
        }

        // debug communicator
        Communicator cm = new Communicator(ids, ac, sum, ind);
        int bb = Integer.valueOf(sCurCateg);
        if (bb == -1) {
            bb = 0;
        }
        // print summary from communicator
        int iSummarizedClusterCnt = 0;
        for (Topic tTopic : ac.getArticlesPerCluster().values()) {
            if (tTopic.size() > 1) {
                System.out.println("Printing summary for topic: " + tTopic.getTitle());
                String[] eachSnippet = cm.getSummary(tTopic.getID(), "All").split(cm.getFirstLevelSeparator());
                int iAllTmpSourcesCount = eachSnippet[0].split(cm.getSecondLevelSeparator()).length;
                System.out.println("With Summary Sources: " + iAllTmpSourcesCount);
                for (int i = 1; i < eachSnippet.length; i++) {
                    String[] eachSent = eachSnippet[i].split(cm.getSecondLevelSeparator());
                    System.out.println(eachSent[0]);
                    System.out.println("-----------------------------------");
                    iSummarizedClusterCnt++;
                }
                System.out.println("===========================");
            }
        }

    }
    //DEBUG LINES //get user input
    //        System.out.println("Enter Search String\n");
    //        Scanner imp = new Scanner(System.in);
    //        String term = imp.next();
    //

    //        String sTop = cm.getTopicIDsByKeyword(ind, term, "All");
    //        System.out.println(sTop);
    //        System.out.println(cm.getTopicTitlesByIDs(sTop));

    // last debug
    //        String sUserSources = "http://rss.in.gr/feed/news/culture/" +
    //            "http://www.tovima.gr/feed/culture/" +
    //            "http://www.naftemporiki.gr/rssFeed?mode=section&id=6&atype=story";
    //        System.out.println(cm.getTopics(sUserSources, (String) al.get(bb)));
    //        System.out.println(cm.getTopics("All", (String) al.get(bb)));
    // last debug

    //        System.out.println("Found a total of " + iSummarizedClusterCnt + " summaries"
    //                + " from more than one texts.");
    //        System.out.println(cm.getTopicIDs("All", (String) al.get(bb)));
    //        System.out.println("===============printing topic titles");
    //        System.out.println(cm.getTopicTitles("All", (String) al.get(bb)));
    //        System.out.println("===============ending printing topic titles");
    //        String sUserSources = "http://rss.in.gr/feed/news/world/;;;"
    //                + "http://www.naftemporiki.gr/news/static/rss/news_pol_pol-world.xml;;;"
    //                + "http://ws.kathimerini.gr/xml_files/worldnews.xml;;;"
    //                + "http://feeds.feedburner.com/skai/aqOL?format=xml";
    //        System.out.println(cm.getTopicIDs(sUserSources, (String) al.get(bb)));
    //        System.out.println(cm.getTopicTitles(sUserSources, (String) al.get(bb)));

    //        int counter = 0;
    //        for (Topic tTopic : ac.getArticlesPerCluster().values()) {
    //            System.out.println("====================");
    //            System.out.println(cm.getSummary(tTopic.getID(), sUserSources));
    //            counter ++;
    //            if (counter == 3) {
    //                break;
    //            }
    //        }

}

From source file:havocx42.Program.java

private static void initRootLogger() throws SecurityException, IOException {

    FileHandler fileHandler;//from w w w .j a  v  a2s .c  o m
    fileHandler = new FileHandler("PRWeaponStats.%u.%g.txt", 1024 * 1024 * 8, 3, false);
    fileHandler.setLevel(Level.FINEST);
    fileHandler.setFormatter(new SimpleFormatter());
    Logger rootLogger = Logger.getLogger("");
    Handler[] handlers = rootLogger.getHandlers();
    for (Handler handler : handlers) {
        handler.setLevel(Level.INFO);
    }
    rootLogger.setLevel(Level.FINEST);
    rootLogger.addHandler(fileHandler);
}

From source file:velo.patterns.FactoryLogger.java

/**
 * Factory a logger by a log file name/*from   w  ww .  j a  va  2 s  .c  o  m*/
 * @param logName The log file name to factory a logger object for
 * @return A logger object for the specified log file name
 */
public static Logger loggerFactory(String logName) {
    Logger logger = Logger.getLogger(logName);
    //Create a new handler that uses the simple formatter
    try {
        FileHandler fh = new FileHandler(getLogDir() + logName + ".txt");
        fh.setFormatter(new SimpleFormatter());
        logger.addHandler(fh);
    } catch (IOException e) {
    }

    return logger;
}

From source file:util.Log.java

/**
 * Gera um arquivo log dentro da Pasta Logger que fica na Pasta principal do
 * Usuario./*from  w w  w.  j  a va 2 s.  co  m*/
 *
 * @param className
 * @param ex
 */
public static void relatarExcecao(String className, Exception ex) {
    try {
        /*
         * Informamos qual o nome do Logger, que no caso vai ser o nome da
         * Classe que acontecer a exceo
         */
        Logger log = Logger.getLogger(className);
        /*
         * Variavel que vai conter qual a pasta do sistema que liga ao
         * usuario, por padro ser do sistema operacional Windows
         */
        String systemPath = "/Users/";
        /* Se for outro sistema operacional */
        if (System.getProperty("os.name").startsWith("Linux")) {
            systemPath = "/home/";
        }
        /* Pasta onde vamos colocar os Logs */
        File pastaLog = new File(systemPath + System.getProperty("user.name") + "/Logger");
        if (!pastaLog.exists()) {
            pastaLog.mkdir();
        }
        String arquivoDir = pastaLog.getAbsolutePath() + "/LOG_"
                + LocalDateTime.now().format(DateTimeFormatter.ofPattern("dd-MM_HH-mm-ss")) + ".log";
        /* Classe responsavel por escrever o arquivo */
        FileHandler escrever = new FileHandler(arquivoDir, true);
        /*
         * Precisamos informar como ser escrito(formato) as excees, Vamos
         * Utilizar uma Classe j pronta para isso a Classe SimpleFormatter
         */
        escrever.setFormatter(new SimpleFormatter());
        /*
         * Adicionamos ao nosso log a Classe que vai escrever a exceo que
         * for gerada
         */
        log.addHandler(escrever);

        /*
         * Geramos o Log, passamos que ser de Nivel Severe(Alto), e
         * passamos a exceo para ele
         */
        log.log(Level.SEVERE, null, ex);

        /* Finalizamos a escrita */
        escrever.flush();
        escrever.close();
        /*Envia por email a exceo*/
        Log.relatarExceptionEmail(className, ex.getMessage(), arquivoDir);

    } catch (IOException | SecurityException e) {
        Logger.getLogger(Log.class.getName()).log(Level.SEVERE, null, e);
    }
}

From source file:org.xframium.device.logging.ThreadedFileHandler.java

public void configureHandler(Level baseLevel) {
    setLevel(baseLevel);//from   ww w  .  j a va2s .  c o m
    setFormatter(new SimpleFormatter());

    LogManager.getLogManager().getLogger("").addHandler(this);
    LogManager.getLogManager().getLogger("").setLevel(baseLevel);
    if (LogManager.getLogManager().getLogger(X_NAMESPACE) != null) {
        LogManager.getLogManager().getLogger(X_NAMESPACE).setLevel(baseLevel);
        LogManager.getLogManager().getLogger(X_NAMESPACE).addHandler(this);
    }
}

From source file:regextester.RegexTester.java

public RegexTester() throws Exception {
    RegexTester.application = this;

    // First setup our logger
    this.checkSettingsDirExists();
    logger.setLevel(Level.SEVERE);
    FileHandler fh = new FileHandler(this.settingsDirname + File.separator + "debug.log", 8096, 1, true);
    logger.addHandler(fh);// w w  w. jav  a2  s .co  m
    SimpleFormatter formatter = new SimpleFormatter();
    fh.setFormatter(formatter);
    logger.info("Starting RegexTester");

    // Load our properties and attach the database, creating it if it doesn't exist
    this.loadProperties();

    // Create our GUI
    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    window = new MainWindow(this);
    window.setLocationRelativeTo(null);
    window.setTitle("RegexTester");
    window.setVisible(true);
}

From source file:com.screenslicer.common.Log.java

public static void init(String loggerName, boolean allowFileLogging) {
    logger = Logger.getLogger(loggerName);
    if (allowFileLogging) {
        FileHandler fh = null;/*from w ww  .  jav  a  2  s . c o  m*/
        try {
            fh = new FileHandler("../" + loggerName + ".log", 250000, 9, true);
            logger.addHandler(fh);
            String logLevel = System.getProperty("screenslicer-logging", "prod");
            if (logLevel.equals("prod")) {
                logger.setLevel(Level.INFO);
            } else if (logLevel.equals("debug")) {
                logger.setLevel(Level.ALL);
            }
            SimpleFormatter formatter = new SimpleFormatter();
            fh.setFormatter(formatter);
        } catch (Throwable t) {
            t.printStackTrace();
            throw new RuntimeException(t);
        }
    }
}

From source file:net.tradelib.apps.StrategyBacktest.java

public static void run(Strategy strategy) throws Exception {
    // Setup the logging
    System.setProperty("java.util.logging.SimpleFormatter.format",
            "%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS: %4$s: %5$s%n%6$s%n");
    LogManager.getLogManager().reset();
    Logger rootLogger = Logger.getLogger("");
    if (Boolean.parseBoolean(BacktestCfg.instance().getProperty("file.log", "true"))) {
        FileHandler logHandler = new FileHandler("diag.out", 8 * 1024 * 1024, 2, true);
        logHandler.setFormatter(new SimpleFormatter());
        logHandler.setLevel(Level.FINEST);
        rootLogger.addHandler(logHandler);
    }/*  w  w w  .ja  va  2  s. c  o  m*/

    if (Boolean.parseBoolean(BacktestCfg.instance().getProperty("console.log", "true"))) {
        ConsoleHandler consoleHandler = new ConsoleHandler();
        consoleHandler.setFormatter(new SimpleFormatter());
        consoleHandler.setLevel(Level.INFO);
        rootLogger.addHandler(consoleHandler);
    }

    rootLogger.setLevel(Level.INFO);

    // Setup Hibernate
    // Configuration configuration = new Configuration();
    // StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
    // SessionFactory factory = configuration.buildSessionFactory(builder.build());

    Context context = new Context();
    context.dbUrl = BacktestCfg.instance().getProperty("db.url");

    HistoricalDataFeed hdf = new SQLDataFeed(context);
    hdf.configure(BacktestCfg.instance().getProperty("datafeed.config", "config/datafeed.properties"));
    context.historicalDataFeed = hdf;

    HistoricalReplay hr = new HistoricalReplay(context);
    context.broker = hr;

    strategy.initialize(context);
    strategy.cleanupDb();

    long start = System.nanoTime();
    strategy.start();
    long elapsedTime = System.nanoTime() - start;
    System.out.println("backtest took " + String.format("%.2f secs", (double) elapsedTime / 1e9));

    start = System.nanoTime();
    strategy.updateEndEquity();
    strategy.writeExecutionsAndTrades();
    strategy.writeEquity();
    elapsedTime = System.nanoTime() - start;
    System.out
            .println("writing to the database took " + String.format("%.2f secs", (double) elapsedTime / 1e9));

    System.out.println();

    // Write the strategy totals to the database
    strategy.totalTradeStats();

    // Write the strategy report to the database and obtain the JSON
    // for writing it to the console.
    JsonObject report = strategy.writeStrategyReport();

    JsonArray asa = report.getAsJsonArray("annual_stats");

    String csvPath = BacktestCfg.instance().getProperty("positions.csv.prefix");
    if (!Strings.isNullOrEmpty(csvPath)) {
        csvPath += "-" + strategy.getLastTimestamp().toLocalDate().format(DateTimeFormatter.BASIC_ISO_DATE)
                + ".csv";
    }

    String ordersCsvPath = BacktestCfg.instance().getProperty("orders.csv.suffix");
    if (!Strings.isNullOrEmpty(ordersCsvPath)) {
        ordersCsvPath = strategy.getLastTimestamp().toLocalDate().format(DateTimeFormatter.BASIC_ISO_DATE) + "-"
                + strategy.getName() + ordersCsvPath;
    }

    String actionsPath = BacktestCfg.instance().getProperty("actions.file.suffix");
    if (!Strings.isNullOrEmpty(actionsPath)) {
        actionsPath = strategy.getLastTimestamp().toLocalDate().format(DateTimeFormatter.BASIC_ISO_DATE) + "-"
                + strategy.getName() + actionsPath;
    }

    // If emails are being send out
    String signalText = StrategyText.build(context.dbUrl, strategy.getName(),
            strategy.getLastTimestamp().toLocalDate(), "   ", csvPath, '|');

    System.out.println(signalText);
    System.out.println();

    if (!Strings.isNullOrEmpty(ordersCsvPath)) {
        StrategyText.buildOrdersCsv(context.dbUrl, strategy.getName(),
                strategy.getLastTimestamp().toLocalDate(), ordersCsvPath);
    }

    File actionsFile = Strings.isNullOrEmpty(actionsPath) ? null : new File(actionsPath);

    if (actionsFile != null) {
        FileUtils.writeStringToFile(actionsFile,
                signalText + System.getProperty("line.separator") + System.getProperty("line.separator"));
    }

    String message = "";

    if (asa.size() > 0) {
        // Sort the array
        TreeMap<Integer, Integer> map = new TreeMap<Integer, Integer>();
        for (int ii = 0; ii < asa.size(); ++ii) {
            int year = asa.get(ii).getAsJsonObject().get("year").getAsInt();
            map.put(year, ii);
        }

        for (int id : map.values()) {
            JsonObject jo = asa.get(id).getAsJsonObject();
            String yearStr = String.valueOf(jo.get("year").getAsInt());
            String pnlStr = String.format("$%,d", jo.get("pnl").getAsInt());
            String pnlPctStr = String.format("%.2f%%", jo.get("pnl_pct").getAsDouble());
            String endEqStr = String.format("$%,d", jo.get("end_equity").getAsInt());
            String ddStr = String.format("$%,d", jo.get("maxdd").getAsInt());
            String ddPctStr = String.format("%.2f%%", jo.get("maxdd_pct").getAsDouble());
            String str = yearStr + " PnL: " + pnlStr + ", PnL Pct: " + pnlPctStr + ", End Equity: " + endEqStr
                    + ", MaxDD: " + ddStr + ", Pct MaxDD: " + ddPctStr;
            message += str + "\n";
        }

        String pnlStr = String.format("$%,d", report.get("pnl").getAsInt());
        String pnlPctStr = String.format("%.2f%%", report.get("pnl_pct").getAsDouble());
        String ddStr = String.format("$%,d", report.get("avgdd").getAsInt());
        String ddPctStr = String.format("%.2f%%", report.get("avgdd_pct").getAsDouble());
        String gainToPainStr = String.format("%.4f", report.get("gain_to_pain").getAsDouble());
        String str = "\nAvg PnL: " + pnlStr + ", Pct Avg PnL: " + pnlPctStr + ", Avg DD: " + ddStr
                + ", Pct Avg DD: " + ddPctStr + ", Gain to Pain: " + gainToPainStr;
        message += str + "\n";
    } else {
        message += "\n";
    }

    // Global statistics
    JsonObject jo = report.getAsJsonObject("total_peak");
    String dateStr = jo.get("date").getAsString();
    int maxEndEq = jo.get("equity").getAsInt();
    jo = report.getAsJsonObject("total_maxdd");
    double cash = jo.get("cash").getAsDouble();
    double pct = jo.get("pct").getAsDouble();
    message += "\n" + "Total equity peak [" + dateStr + "]: " + String.format("$%,d", maxEndEq) + "\n"
            + String.format("Current Drawdown: $%,d [%.2f%%]", Math.round(cash), pct) + "\n";

    if (report.has("latest_peak") && report.has("latest_maxdd")) {
        jo = report.getAsJsonObject("latest_peak");
        LocalDate ld = LocalDate.parse(jo.get("date").getAsString(), DateTimeFormatter.ISO_DATE);
        maxEndEq = jo.get("equity").getAsInt();
        jo = report.getAsJsonObject("latest_maxdd");
        cash = jo.get("cash").getAsDouble();
        pct = jo.get("pct").getAsDouble();
        message += "\n" + Integer.toString(ld.getYear()) + " equity peak ["
                + ld.format(DateTimeFormatter.ISO_DATE) + "]: " + String.format("$%,d", maxEndEq) + "\n"
                + String.format("Current Drawdown: $%,d [%.2f%%]", Math.round(cash), pct) + "\n";
    }

    message += "\n" + "Avg Trade PnL: "
            + String.format("$%,d", Math.round(report.get("avg_trade_pnl").getAsDouble())) + ", Max DD: "
            + String.format("$%,d", Math.round(report.get("maxdd").getAsDouble())) + ", Max DD Pct: "
            + String.format("%.2f%%", report.get("maxdd_pct").getAsDouble()) + ", Num Trades: "
            + Integer.toString(report.get("num_trades").getAsInt());

    System.out.println(message);

    if (actionsFile != null) {
        FileUtils.writeStringToFile(actionsFile, message + System.getProperty("line.separator"), true);
    }

    if (Boolean.parseBoolean(BacktestCfg.instance().getProperty("email.enabled", "false"))) {
        Properties props = new Properties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", "smtp.sendgrid.net");
        props.put("mail.smtp.port", "587");

        String user = BacktestCfg.instance().getProperty("email.user");
        String pass = BacktestCfg.instance().getProperty("email.pass");

        Session session = Session.getInstance(props, new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(user, pass);
            }
        });

        MimeMessage msg = new MimeMessage(session);
        try {
            msg.setFrom(new InternetAddress(BacktestCfg.instance().getProperty("email.from")));
            msg.addRecipients(RecipientType.TO, BacktestCfg.instance().getProperty("email.recipients"));
            msg.setSubject(strategy.getName() + " Report ["
                    + strategy.getLastTimestamp().format(DateTimeFormatter.ISO_LOCAL_DATE) + "]");
            msg.setText("Positions & Signals\n" + signalText + "\n\nStatistics\n" + message);
            Transport.send(msg);
        } catch (Exception ee) {
            Logger.getLogger("").warning(ee.getMessage());
        }
    }

    if (Boolean.parseBoolean(BacktestCfg.instance().getProperty("sftp.enabled", "false"))) {
        HashMap<String, String> fileMap = new HashMap<String, String>();
        if (!Strings.isNullOrEmpty(actionsPath))
            fileMap.put(actionsPath, actionsPath);
        if (!Strings.isNullOrEmpty(ordersCsvPath))
            fileMap.put(ordersCsvPath, ordersCsvPath);
        String user = BacktestCfg.instance().getProperty("sftp.user");
        String pass = BacktestCfg.instance().getProperty("sftp.pass");
        String host = BacktestCfg.instance().getProperty("sftp.host");
        SftpUploader sftp = new SftpUploader(host, user, pass);
        sftp.upload(fileMap);
    }
}

From source file:namedatabasescraper.NameDatabaseScraper.java

public NameDatabaseScraper() throws Exception {
    NameDatabaseScraper.application = this;

    // First setup our logger
    this.checkSettingsDirExists();
    logger.setLevel(Level.INFO);/*from   ww  w. jav a  2  s  .co m*/
    FileHandler fh = new FileHandler(this.settingsDirname + File.separator + "debug.log", 8096, 1, false);
    logger.addHandler(fh);
    SimpleFormatter formatter = new SimpleFormatter();
    fh.setFormatter(formatter);
    logger.info("Starting NameDatabaseScraper");

    // Load our properties and attach the database, creating it if it doesn't exist
    this.loadProperties();
    this.attachDatabase();

    // Create our GUI
    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    window = new MainWindow(this);
    window.setLocationRelativeTo(null);
    window.setTitle("NameDatabaseScraper");
    window.setVisible(true);
}