AccountingFormatter.java 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Copyright (C) 2004-2014 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.log.formatter;
  20. import java.text.SimpleDateFormat;
  21. import java.util.Date;
  22. import java.util.logging.Formatter;
  23. import java.util.logging.LogRecord;
  24. import com.l2jserver.Config;
  25. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  26. import com.l2jserver.gameserver.network.L2GameClient;
  27. import com.l2jserver.util.StringUtil;
  28. public class AccountingFormatter extends Formatter
  29. {
  30. private final SimpleDateFormat dateFmt = new SimpleDateFormat("dd MMM H:mm:ss");
  31. @Override
  32. public String format(LogRecord record)
  33. {
  34. final Object[] params = record.getParameters();
  35. final StringBuilder output = StringUtil.startAppend(30 + record.getMessage().length() + (params == null ? 0 : params.length * 10), "[", dateFmt.format(new Date(record.getMillis())), "] ", record.getMessage());
  36. if (params != null)
  37. {
  38. for (Object p : params)
  39. {
  40. if (p == null)
  41. {
  42. continue;
  43. }
  44. StringUtil.append(output, ", ");
  45. if (p instanceof L2GameClient)
  46. {
  47. final L2GameClient client = (L2GameClient) p;
  48. String address = null;
  49. try
  50. {
  51. if (!client.isDetached())
  52. {
  53. address = client.getConnection().getInetAddress().getHostAddress();
  54. }
  55. }
  56. catch (Exception e)
  57. {
  58. }
  59. switch (client.getState())
  60. {
  61. case IN_GAME:
  62. if (client.getActiveChar() != null)
  63. {
  64. StringUtil.append(output, client.getActiveChar().getName());
  65. StringUtil.append(output, "(", String.valueOf(client.getActiveChar().getObjectId()), ") ");
  66. }
  67. case AUTHED:
  68. if (client.getAccountName() != null)
  69. {
  70. StringUtil.append(output, client.getAccountName(), " ");
  71. }
  72. case CONNECTED:
  73. if (address != null)
  74. {
  75. StringUtil.append(output, address);
  76. }
  77. break;
  78. default:
  79. throw new IllegalStateException("Missing state on switch");
  80. }
  81. }
  82. else if (p instanceof L2PcInstance)
  83. {
  84. L2PcInstance player = (L2PcInstance) p;
  85. StringUtil.append(output, player.getName());
  86. StringUtil.append(output, "(", String.valueOf(player.getObjectId()), ")");
  87. }
  88. else
  89. {
  90. StringUtil.append(output, p.toString());
  91. }
  92. }
  93. }
  94. output.append(Config.EOL);
  95. return output.toString();
  96. }
  97. }