Creating a log

Discussion in 'Plugin Development' started by Retherz_, Nov 6, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    Retherz_

    Hello, im trying to create a log to log a message but it doesn't work anyone know a better way/fix?
    Code:
    public void onEnable(){
    PluginDescriptionFile pdfFile = getDescription();
    logger.info("["+pdfFile.getName()+"] version "+pdfFile.getVersion()+" has been enabled!");
    Bukkit.getPluginManager().registerEvents(this, this);
    for(Handler h : notifylog.getHandlers())
    {
    notifylog.removeHandler(h);
    }
    try
    {
    notifylog.addHandler(new FileHandler(this.getDataFolder()+"/orenotify.log"));
    }
    catch(IOException e)
    {
    logger.info("Failed to create the log");
    e.printStackTrace();
    }
    }
    @EventHandler
    public void breakblock(BlockBreakEvent event) {
    Player player = event.getPlayer();
    Material block = event.getBlock().getType();
    if (block == Material.DIAMOND_ORE || block == Material.IRON_ORE || block == Material.GOLD_ORE || block == Material.EMERALD_ORE || block == Material.REDSTONE_ORE) {
    for (Player p : Bukkit.getServer().getOnlinePlayers()) {
    if (p.hasPermission("notify.orenotify")) {
    p.sendMessage(ChatColor.GOLD + player.getName() + ChatColor.DARK_AQUA + " has mined " + ChatColor.LIGHT_PURPLE + Material.getMaterial(block.getId()));
    }
    }
    notifylog.info(player.getName()+ " has mined "+Material.getMaterial(block.getId()));// <-- i want to log this
    }
    }
    }
     
  2. Offline

    JJSfluffyduck

    Did you want the log to be saved in a config or printed on screen to ops only or just to the cmd line?
     
  3. Offline

    Retherz_

    JJSfluffyduck Saved in a log, told to everyone with the permission
     
  4. Offline

    JJSfluffyduck

    Does it give you a error if so where?
     
  5. Offline

    ZeusAllMighty11

    I like to use YML files, personally I find them pretty easy. Or sql tables


    What type of file does it log to?
     
  6. Offline

    Sagacious_Zed Bukkit Docs

    YAML makes a bad log, because the entire log from the beginning of the file needs to be kept in memory.
     
  7. Offline

    fireblast709

    MercilessPvP I have tested the code, and it just works for me. Are you still getting the IOException: cannot get lock?
     
  8. Offline

    Retherz_

  9. Offline

    Lolmewn

    I say MySQL. :D
     
  10. Offline

    fireblast709

    Lolmewn I say stop making suggestions that make no sense for low usage loggers :3 (no seriously, it would make no sense to use a database here)
     
  11. Offline

    Retherz_

    does anyone know whats wrong?
     
  12. Offline

    Lolmewn

    Does the file ever get created?
     
  13. Offline

    Retherz_

    No it doesnt

    Lolmewn i dont know what is wrong

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
  14. Offline

    fireblast709

    MercilessPvP aaah finally the error. I think I might have found your fix (replace your part of the code with this):
    Code:java
    1. for(Handler h : blockbreak.getHandlers())
    2. {
    3. blockbreak.removeHandler(h);
    4. }
    5. try
    6. {
    7. File f = this.getDataFolder();
    8. if(!f.exists())
    9. {
    10. if(!f.mkdir())
    11. {
    12. throw new IOException("Could not create folder '/plugins/"+"TestPlugin"+"'");
    13. }
    14. else
    15. {
    16. log.info("created datafolder");
    17. }
    18. }
    19. else
    20. {
    21. log.info("folder exists: "+f.getAbsolutePath());
    22. }
    23. FileHandler fh = new FileHandler(this.getDataFolder()+File.separator+"orenotify.log");
    24. blockbreak.addHandler(fh);
    25. blockbreak.setUseParentHandlers(false);
    26. }
    27. catch(IOException e)
    28. {
    29. blockbreak.info("Failed to create the log");
    30. e.printStackTrace();
    31. }

    The problem was that your datafolder did not exists, and this code also checks if it does. If it does not, it attempts to create the directory :3. Tested and confirmed
     
    MercilessPvP likes this.
  15. Offline

    Retherz_

  16. Offline

    fireblast709

    MercilessPvP no problem, actually figuring this out learned me something as well ;3
     
    MercilessPvP likes this.
  17. Offline

    Retherz_

    fireblast709 am i supose to replace
    Code:
            for(Handler h : notifylog.getHandlers())
            {
                notifylog.removeHandler(h);
            }
            try
            {
                notifylog.addHandler(new FileHandler(this.getDataFolder()+"/orenotify.log"));
            }
            catch(IOException e)
            {
                logger.info("Failed to create the log");
                e.printStackTrace();
            }
        }
    ? cause if i do that i get many errors
     
  18. Offline

    fireblast709

    lol you have replaced a } too much I think. A small 'gift' from me, your correct onEnable():
    Code:java
    1. public void onEnable()
    2. {
    3. PluginDescriptionFile pdfFile = getDescription();
    4. logger.info("["+pdfFile.getName()+"] version "+pdfFile.getVersion()+" has been enabled!");
    5. Bukkit.getPluginManager().registerEvents(this, this);
    6. for(Handler h : blockbreak.getHandlers())
    7. {
    8. blockbreak.removeHandler(h);
    9. }
    10. try
    11. {
    12. File f = this.getDataFolder();
    13. if(!f.exists())
    14. {
    15. if(!f.mkdir())
    16. {
    17. throw new IOException("Could not create folder '/plugins/"+"TestPlugin"+"'");
    18. }
    19. else
    20. {
    21. log.info("created datafolder");
    22. }
    23. }
    24. else
    25. {
    26. log.info("folder exists: "+f.getAbsolutePath());
    27. }
    28. FileHandler fh = new FileHandler(this.getDataFolder()+File.separator+"orenotify.log");
    29. blockbreak.addHandler(fh);
    30. blockbreak.setUseParentHandlers(false);
    31. }
    32. catch(IOException e)
    33. {
    34. blockbreak.info("Failed to create the log");
    35. e.printStackTrace();
    36. }
    37. }
     
  19. Offline

    Retherz_

    fireblast709 i get errors i can upload an image

    fireblast709[​IMG]

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
  20. Offline

    fireblast709

  21. Offline

    Retherz_

  22. Offline

    fireblast709

    MercilessPvP O that... replace log with logger, and blockbreak with notifylog
     
  23. Offline

    Retherz_

    fireblast709 thanks :)

    fireblast709 what about File?

    fireblast709 import?

    import java.io.File;

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
  24. Offline

    fireblast709

  25. Offline

    Retherz_

    fireblast709 hello, i am getting this:
    Code:
    <?xml version="1.0" encoding="windows-1252" standalone="no"?>
    <!DOCTYPE log SYSTEM "logger.dtd">
    <log>
    <record>
      <date>2012-11-07T20:27:32</date>
      <millis>1352316452168</millis>
      <sequence>47</sequence>
      <logger>orenotify</logger>
      <level>INFO</level>
      <class>me.mercilesspvp.orenotify.orenotify</class>
      <method>breakblock</method>
      <thread>13</thread>
      <message>Player has mined IRON_ORE</message>
    </record>
    <record>
      <date>2012-11-07T20:27:33</date>
      <millis>1352316453192</millis>
      <sequence>48</sequence>
      <logger>orenotify</logger>
      <level>INFO</level>
      <class>me.mercilesspvp.orenotify.orenotify</class>
      <method>breakblock</method>
      <thread>13</thread>
      <message>Player has mined DIAMOND_ORE</message>
    </record>
    <record>
      <date>2012-11-07T20:27:34</date>
      <millis>1352316454361</millis>
      <sequence>49</sequence>
      <logger>orenotify</logger>
      <level>INFO</level>
      <class>me.mercilesspvp.orenotify.orenotify</class>
      <method>breakblock</method>
      <thread>13</thread>
      <message>Player has mined DIAMOND_ORE</message>
    </record>
    <record>
      <date>2012-11-07T20:27:36</date>
      <millis>1352316456009</millis>
      <sequence>50</sequence>
      <logger>orenotify</logger>
      <level>INFO</level>
      <class>me.mercilesspvp.orenotify.orenotify</class>
      <method>breakblock</method>
      <thread>13</thread>
      <message>Player has mined DIAMOND_ORE</message>
    </record>
    <record>
      <date>2012-11-07T20:27:37</date>
      <millis>1352316457155</millis>
      <sequence>51</sequence>
      <logger>orenotify</logger>
      <level>INFO</level>
      <class>me.mercilesspvp.orenotify.orenotify</class>
      <method>breakblock</method>
      <thread>13</thread>
      <message>Player has mined DIAMOND_ORE</message>
    </record>
    <record>
      <date>2012-11-07T20:27:38</date>
      <millis>1352316458010</millis>
      <sequence>52</sequence>
      <logger>orenotify</logger>
      <level>INFO</level>
      <class>me.mercilesspvp.orenotify.orenotify</class>
      <method>breakblock</method>
      <thread>13</thread>
      <message>Player has mined DIAMOND_ORE</message>
    </record>
    <record>
      <date>2012-11-07T20:27:39</date>
      <millis>1352316459054</millis>
      <sequence>53</sequence>
      <logger>orenotify</logger>
      <level>INFO</level>
      <class>me.mercilesspvp.orenotify.orenotify</class>
      <method>breakblock</method>
      <thread>13</thread>
      <message>Player has mined DIAMOND_ORE</message>
    </record>
    <record>
      <date>2012-11-07T20:27:40</date>
      <millis>1352316460005</millis>
      <sequence>54</sequence>
      <logger>orenotify</logger>
      <level>INFO</level>
      <class>me.mercilesspvp.orenotify.orenotify</class>
      <method>breakblock</method>
      <thread>13</thread>
      <message>Player has mined DIAMOND_ORE</message>
    </record>
    </log>
    
     
  26. Offline

    fireblast709

    MercilessPvP I figured that would happen. Wrote you a formatter:
    Code:java
    1. class NotifyFormat extends Formatter
    2. {
    3. @Override
    4. public String format(LogRecord record)
    5. {
    6. Date d = new Date(record.getMillis());
    7. String time = d.toString();
    8. return "["+time+"]: "+record.getMessage();
    9. }
    10. }

    Add the class to the main instance (after the main class to be exact) and add this:
    Code:java
    1. // You have the next line
    2. FileHandler fh = new FileHandler(this.getDataFolder()+File.separator+"orenotify.log");
    3. // Now after that line, add:
    4. fh.setFormatter(new NotifyFormat())
     
Thread Status:
Not open for further replies.

Share This Page