SpringBoot Rest Api应用程序日志未在fluentdContainer的日志文件夹中创建

时间:2019-07-27 04:03:34

标签: spring-boot fluentd

我有一个Spring Boot Rest应用程序,我正在使用FludentD Docker映像,并尝试使用FluentD自定义附加程序将日志发送到fluentd日志文件夹。但是日志没有被传输。我在这里有一些日志语句。我不确定我在做什么错。我的Rest API具有简单的终点 GET:http://localhost:8082/account/accounts。有人可以帮忙吗?

package com.revolut.coding.accountapi.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.revolut.coding.accountapi.exception.InvalidTransferException;
import com.revolut.coding.accountapi.exception.NotEnoughMoneyException;
import com.revolut.coding.accountapi.model.Account;
import com.revolut.coding.accountapi.repository.AccountRepository;
import com.revolut.coding.accountapi.service.AccountService;


@Service
@Slf4j
public class AccountServiceImpl implements AccountService{

  @Autowired
  private AccountRepository accountRepository;

  @Override
  public List<Account> getAllAccounts() {
    // return Accounts;
    List<Account> accounts= new ArrayList<Account>();

          log.trace("doStuff needed more information - {}", "test");
          log.debug("doStuff needed to debug - {}", "test");
          log.info("doStuff took input - {}", "test");
          log.warn("doStuff needed to warn - {}", "test");
          log.error("doStuff encountered an error with value - {}", "test");

    accountRepository.findAll()
    .forEach(accounts::add);
    return accounts;
  }


  @Override
  public Optional<Account> getAccount(long id) {
    // return an account;
    return accountRepository.findById(id);
  }

  @Override
  public long createAccount(Account account) {
    // create account
     accountRepository.save(account);
     return account.getId();
  }

  @Override
  public List<Account> transferAmountFromOneAccountToAnother(long fromId, long toId, long amount) throws NotEnoughMoneyException, InvalidTransferException{
    // return Debited Account and Credited Account
    List<Account> accounts= new ArrayList<Account>();
    accounts.add(accountRepository.findById(fromId).get());
    accounts.add(accountRepository.findById(toId).get());
    Account accFrom = accounts.stream().filter(a->(a.getId()==fromId)).findFirst().get();   
     if (accFrom == null) {
       throw new InvalidTransferException("One of accounts not found");
     }
    if((accFrom.getAmount()-amount )<0)
    {
      throw new NotEnoughMoneyException("Not enough funds in Account");
    }
     accFrom.setAmount(accFrom.getAmount()-amount);
     accountRepository.save(accFrom);

     Account accTo = accounts.stream().filter(a->(a.getId()==toId)).findFirst().get();  
     if (accTo == null) {
       throw new InvalidTransferException("One of accounts not found");
     }
     accTo.setAmount(accTo.getAmount()+amount);
     accountRepository.save(accTo); 

    return accounts;
  }

  @Override
  public Account depositAmount(long id, long amount) {

    List<Account> accounts= new ArrayList<Account>();
    accountRepository.findAll()
    .forEach(accounts::add);
    Account acc = accounts.stream().filter(a->(a.getId()==id)).findFirst().get();   
     if (acc == null) {
       return null;
     }
     acc.setAmount(acc.getAmount()+amount);
     return accountRepository.save(acc);

  }

  @Override
  public void deleteAccount(long id) {
    accountRepository.deleteById(id);

  } 
}
<?xml version="1.0" encoding="UTF-8"?>
<configuration>

<appender name="FLUENT_APPENDER" class="com.test.CustomFluentAppender">
    <remoteHost>localhost</remoteHost>
    <port>24224</port>
    <tag>test</tag>
    <includeThreadName>false</includeThreadName>
    <includeLoggerName>false</includeLoggerName>
    <includeFormattedMessage>true</includeFormattedMessage>
    <includeMessage>false</includeMessage>
    <includeException>true</includeException>
</appender>

<!-- Log output configuration -->
<root level="INFO">
    <appender-ref ref="FLUENT_APPENDER"/>
</root>
</configuration>
<source>
@type  forward
@id    input1
@label @mainstream
port  24224
</source>

<filter **>
@type stdout
</filter>

<label @mainstream>
<match **>
@type file
@id   output_do
path         /fluentd/log/data.*.log
symlink_path /fluentd/log/data.log
append       true
time_slice_format %Y%m%d
time_slice_wait   1m
time_format       %Y%m%dT%H%M%S%z
</match>
</label>

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.revolut.coding.accountapi.exception.InvalidTransferException;
import com.revolut.coding.accountapi.exception.NotEnoughMoneyException;
import com.revolut.coding.accountapi.model.Account;
import com.revolut.coding.accountapi.repository.AccountRepository;
import com.revolut.coding.accountapi.service.AccountService;


@Service
@Slf4j
public class AccountServiceImpl implements AccountService{

@Autowired
private AccountRepository accountRepository;

@Override
public List<Account> getAllAccounts() {
// return Accounts;
List<Account> accounts= new ArrayList<Account>();

      log.trace("doStuff needed more information - {}", "test");
      log.debug("doStuff needed to debug - {}", "test");
      log.info("doStuff took input - {}", "test");
      log.warn("doStuff needed to warn - {}", "test");
      log.error("doStuff encountered an error with value - {}", "test");

accountRepository.findAll()
.forEach(accounts::add);
return accounts;
}


@Override
public Optional<Account> getAccount(long id) {
// return an account;
return accountRepository.findById(id);
}

@Override
public long createAccount(Account account) {
// create account
 accountRepository.save(account);
 return account.getId();
}

@Override
public List<Account> transferAmountFromOneAccountToAnother(long fromId, long toId, long amount) throws NotEnoughMoneyException, InvalidTransferException{
// return Debited Account and Credited Account
List<Account> accounts= new ArrayList<Account>();
accounts.add(accountRepository.findById(fromId).get());
accounts.add(accountRepository.findById(toId).get());
Account accFrom = accounts.stream().filter(a->(a.getId()==fromId)).findFirst().get();   
 if (accFrom == null) {
   throw new InvalidTransferException("One of accounts not found");
 }
if((accFrom.getAmount()-amount )<0)
{
  throw new NotEnoughMoneyException("Not enough funds in Account");
}
 accFrom.setAmount(accFrom.getAmount()-amount);
 accountRepository.save(accFrom);

 Account accTo = accounts.stream().filter(a->(a.getId()==toId)).findFirst().get();  
 if (accTo == null) {
   throw new InvalidTransferException("One of accounts not found");
 }
 accTo.setAmount(accTo.getAmount()+amount);
 accountRepository.save(accTo); 

return accounts;
}

@Override
public Account depositAmount(long id, long amount) {

List<Account> accounts= new ArrayList<Account>();
accountRepository.findAll()
.forEach(accounts::add);
Account acc = accounts.stream().filter(a->(a.getId()==id)).findFirst().get();   
 if (acc == null) {
   return null;
 }
 acc.setAmount(acc.getAmount()+amount);
 return accountRepository.save(acc);

}

@Override
public void deleteAccount(long id) {
accountRepository.deleteById(id);

}   
}

logback.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

<appender name="FLUENT_APPENDER" class="com.test.CustomFluentAppender">
    <remoteHost>localhost</remoteHost>
    <port>24224</port>
    <tag>test</tag>
    <includeThreadName>false</includeThreadName>
    <includeLoggerName>false</includeLoggerName>
    <includeFormattedMessage>true</includeFormattedMessage>
    <includeMessage>false</includeMessage>
    <includeException>true</includeException>
</appender>

<!-- Log output configuration -->
<root level="INFO">
    <appender-ref ref="FLUENT_APPENDER"/>
</root>
</configuration>

fluent.conf放在容器中

<source>
@type  forward
@id    input1
@label @mainstream
port  24224
</source>

<filter **>
@type stdout
</filter>

<label @mainstream>
<match **>
@type file
@id   output_do
path         /fluentd/log/data.*.log
symlink_path /fluentd/log/data.log
append       true
time_slice_format %Y%m%d
time_slice_wait   1m
time_format       %Y%m%dT%H%M%S%z
</match>
</label>

0 个答案:

没有答案
相关问题